Skip to content

Commit

Permalink
Fix: handle 409 errors when editing files (#303)
Browse files Browse the repository at this point in the history
* Fix: handle 409 errors when editing files

* Fix: update v1 file endpoint

* Fix: catch 409 errors for delete file operations

* Fix: catch errors for media files
  • Loading branch information
alexanderleegs authored Sep 23, 2021
1 parent ab63979 commit d870f6e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 14 deletions.
8 changes: 8 additions & 0 deletions classes/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ class File {
} catch (err) {
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}
Expand All @@ -155,6 +159,10 @@ class File {
} catch (err) {
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}
Expand Down
48 changes: 34 additions & 14 deletions classes/MediaFile.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,14 +157,24 @@ class MediaFile {
sha,
}

const resp = await axios.put(endpoint, params, {
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json",
},
})
try {
const resp = await axios.put(endpoint, params, {
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json",
},
})

return { newSha: resp.data.commit.sha }
return { newSha: resp.data.commit.sha }
} catch (err) {
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}

async delete(fileName, sha) {
Expand All @@ -176,13 +186,23 @@ class MediaFile {
sha,
}

await axios.delete(endpoint, {
data: params,
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json",
},
})
try {
await axios.delete(endpoint, {
data: params,
headers: {
Authorization: `token ${this.accessToken}`,
"Content-Type": "application/json",
},
})
} catch (err) {
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions services/db/GitHubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ class GitHubService {
if (err instanceof NotFoundError) throw err
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}
Expand Down Expand Up @@ -173,6 +177,10 @@ class GitHubService {
} catch (err) {
const { status } = err.response
if (status === 404) throw new NotFoundError("File does not exist")
if (status === 409)
throw new ConflictError(
"File has been changed recently, please try again"
)
throw err
}
}
Expand Down

0 comments on commit d870f6e

Please sign in to comment.