From d870f6e5d6112b5538990131d990f985df1594cc Mon Sep 17 00:00:00 2001 From: Alexander Lee Date: Thu, 23 Sep 2021 14:43:57 +0800 Subject: [PATCH] Fix: handle 409 errors when editing files (#303) * 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 --- classes/File.js | 8 ++++++ classes/MediaFile.js | 48 +++++++++++++++++++++++++----------- services/db/GitHubService.js | 8 ++++++ 3 files changed, 50 insertions(+), 14 deletions(-) diff --git a/classes/File.js b/classes/File.js index d60c7f50f..39be6f38a 100644 --- a/classes/File.js +++ b/classes/File.js @@ -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 } } @@ -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 } } diff --git a/classes/MediaFile.js b/classes/MediaFile.js index d7be4ddd3..7a2b4c9ed 100644 --- a/classes/MediaFile.js +++ b/classes/MediaFile.js @@ -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) { @@ -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 + } } } diff --git a/services/db/GitHubService.js b/services/db/GitHubService.js index 940f621f5..cdd763204 100644 --- a/services/db/GitHubService.js +++ b/services/db/GitHubService.js @@ -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 } } @@ -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 } }