Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: add new InputNameConflictError #78

Merged
merged 2 commits into from
Nov 24, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion classes/File.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const validateStatus = require('../utils/axios-utils')

// Import error
const { NotFoundError } = require('../errors/NotFoundError')
const { InputNameConflictError } = require('../errors/InputValidationError')

const GITHUB_ORG_NAME = process.env.GITHUB_ORG_NAME
const BRANCH_REF = process.env.BRANCH_REF
Expand Down Expand Up @@ -78,7 +79,9 @@ class File {

return { sha: resp.data.content.sha }
} catch (err) {
throw err
const status = err.response.status
if (status === 422) throw new InputNameConflictError(fileName)
throw err.response
}
}

Expand Down
14 changes: 14 additions & 0 deletions errors/InputValidationError.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Import base error
const { BaseIsomerError } = require('./BaseError')

class InputNameConflictError extends BaseIsomerError {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the isomerCMS project, we've been trying to switch to a different custom error system where the error is solely identified by the HTTP error code, to avoid a situation where we have way too many custom errors as we did in Homer. In keeping with this new strategy, this should be a ConflictError with an argument in the constructor for the error message.

To standardize the error messages, we can define the types of ConflictError messages and export them. For example, we can have:

const inputNameConflictErrorMsg = (fileName) => `A file with ${fileName} already exists.`

class ConflictError extends BaseIsomerError { ... }

module.exports = {
  inputNameConflictErrorMsg,
  ConflictError,
}

and then throw the error like this:

throw new ConflictError(inputNameConflictErrorMsg(fileName))

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved with 10ab103

constructor (fileName) {
super(
409,
`A file with ${fileName} already exists.`
)
}
}
module.exports = {
InputNameConflictError,
}