Skip to content

Augment error object with metadata and Update Terms Error Response #50

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

Merged
merged 2 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ app.use((err, req, res, next) => {
}
}

if (!_.isUndefined(err.metadata)) {
errorResponse.metadata = err.metadata
}

res.status(status).json(errorResponse)
})

Expand Down
21 changes: 21 additions & 0 deletions docs/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -577,6 +577,9 @@ definitions:
type: string
description: The unauthorized error message.
example: Unable to authenticate the user.
metadata:
type: object
description: freeform metadata object
NotFound:
type: object
description: The not found error entity.
Expand All @@ -585,6 +588,9 @@ definitions:
type: string
description: The not found error message.
example: A resource with the name could not be found.
metadata:
type: object
description: freeform metadata object
ServerError:
type: object
description: The server error entity.
Expand All @@ -596,6 +602,9 @@ definitions:
Something went wrong while processing your request. We’re sorry for
the trouble. We’ve been notified of the error and will correct it as
soon as possible. Please try your request again in a moment.
metadata:
type: object
description: freeform metadata object
ServiceUnavailable:
type: object
description: The server is unavailable
Expand All @@ -604,6 +613,9 @@ definitions:
type: string
description: The server error message.
example: Something went wrong with the server.
metadata:
type: object
description: freeform metadata object
BadRequest:
type: object
description: The bad request error entity.
Expand All @@ -612,6 +624,9 @@ definitions:
type: string
description: The bad request error message.
example: Invalid input.
metadata:
type: object
description: freeform metadata object
Forbidden:
type: object
description: The permission error entity.
Expand All @@ -620,6 +635,9 @@ definitions:
type: string
description: The forbidden error message.
example: You are not allowed to access the request.
metadata:
type: object
description: freeform metadata object
Conflict:
type: object
description: The conflict error entity.
Expand All @@ -630,3 +648,6 @@ definitions:
type: string
description: The conflict error message.
example: Creating a resource with a name already exists.
metadata:
type: object
description: freeform metadata object
4 changes: 3 additions & 1 deletion src/common/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ function createError (name, statusCode) {
* The error constructor
* @param {String} message the error message
* @param {String} [cause] the error cause
* @param {Object} metadata the metadata
* @constructor
*/
function ErrorCtor (message, cause) {
function ErrorCtor (message, cause, metadata) {
Error.call(this)
Error.captureStackTrace(this)
this.message = message || name
this.cause = cause
this.metadata = metadata
this.httpStatus = statusCode
}

Expand Down
7 changes: 6 additions & 1 deletion src/common/helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -430,15 +430,20 @@ function partialMatch (filter, value) {
*/
async function checkAgreedTerms (userId, terms) {
const unAgreedTerms = []
const missingTerms = []
for (const term of terms) {
const res = await getRequest(`${config.TERMS_API_URL}/${term.id}`, { userId })
if (!_.get(res, 'body.agreed', false)) {
unAgreedTerms.push(_.get(res, 'body.title', term))
missingTerms.push({
termId: term.id,
roleId: term.roleId
})
}
}

if (unAgreedTerms.length > 0) {
throw new errors.ForbiddenError(`The user has not yet agreed to the following terms: [${unAgreedTerms.join(', ')}]`)
throw new errors.ForbiddenError(`The user has not yet agreed to the following terms: [${unAgreedTerms.join(', ')}]`, null, { missingTerms })
}
}

Expand Down