diff --git a/app.js b/app.js index f565aab..4a57a85 100644 --- a/app.js +++ b/app.js @@ -92,6 +92,10 @@ app.use((err, req, res, next) => { } } + if (!_.isUndefined(err.metadata)) { + errorResponse.metadata = err.metadata + } + res.status(status).json(errorResponse) }) diff --git a/docs/swagger.yaml b/docs/swagger.yaml index aa1b1fe..f7341c2 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -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. @@ -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. @@ -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 @@ -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. @@ -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. @@ -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. @@ -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 diff --git a/src/common/errors.js b/src/common/errors.js index 9908c72..bdd7ffe 100644 --- a/src/common/errors.js +++ b/src/common/errors.js @@ -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 } diff --git a/src/common/helper.js b/src/common/helper.js index 27e8224..5cc1f2b 100644 --- a/src/common/helper.js +++ b/src/common/helper.js @@ -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 }) } }