Skip to content

Commit a9ac39d

Browse files
authored
Merge pull request #50 from topcoder-platform/issue-350
Augment error object with metadata and Update Terms Error Response
2 parents bbcc913 + a420728 commit a9ac39d

File tree

4 files changed

+34
-2
lines changed

4 files changed

+34
-2
lines changed

app.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ app.use((err, req, res, next) => {
9292
}
9393
}
9494

95+
if (!_.isUndefined(err.metadata)) {
96+
errorResponse.metadata = err.metadata
97+
}
98+
9599
res.status(status).json(errorResponse)
96100
})
97101

docs/swagger.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ definitions:
577577
type: string
578578
description: The unauthorized error message.
579579
example: Unable to authenticate the user.
580+
metadata:
581+
type: object
582+
description: freeform metadata object
580583
NotFound:
581584
type: object
582585
description: The not found error entity.
@@ -585,6 +588,9 @@ definitions:
585588
type: string
586589
description: The not found error message.
587590
example: A resource with the name could not be found.
591+
metadata:
592+
type: object
593+
description: freeform metadata object
588594
ServerError:
589595
type: object
590596
description: The server error entity.
@@ -596,6 +602,9 @@ definitions:
596602
Something went wrong while processing your request. We’re sorry for
597603
the trouble. We’ve been notified of the error and will correct it as
598604
soon as possible. Please try your request again in a moment.
605+
metadata:
606+
type: object
607+
description: freeform metadata object
599608
ServiceUnavailable:
600609
type: object
601610
description: The server is unavailable
@@ -604,6 +613,9 @@ definitions:
604613
type: string
605614
description: The server error message.
606615
example: Something went wrong with the server.
616+
metadata:
617+
type: object
618+
description: freeform metadata object
607619
BadRequest:
608620
type: object
609621
description: The bad request error entity.
@@ -612,6 +624,9 @@ definitions:
612624
type: string
613625
description: The bad request error message.
614626
example: Invalid input.
627+
metadata:
628+
type: object
629+
description: freeform metadata object
615630
Forbidden:
616631
type: object
617632
description: The permission error entity.
@@ -620,6 +635,9 @@ definitions:
620635
type: string
621636
description: The forbidden error message.
622637
example: You are not allowed to access the request.
638+
metadata:
639+
type: object
640+
description: freeform metadata object
623641
Conflict:
624642
type: object
625643
description: The conflict error entity.
@@ -630,3 +648,6 @@ definitions:
630648
type: string
631649
description: The conflict error message.
632650
example: Creating a resource with a name already exists.
651+
metadata:
652+
type: object
653+
description: freeform metadata object

src/common/errors.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@ function createError (name, statusCode) {
1515
* The error constructor
1616
* @param {String} message the error message
1717
* @param {String} [cause] the error cause
18+
* @param {Object} metadata the metadata
1819
* @constructor
1920
*/
20-
function ErrorCtor (message, cause) {
21+
function ErrorCtor (message, cause, metadata) {
2122
Error.call(this)
2223
Error.captureStackTrace(this)
2324
this.message = message || name
2425
this.cause = cause
26+
this.metadata = metadata
2527
this.httpStatus = statusCode
2628
}
2729

src/common/helper.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,20 @@ function partialMatch (filter, value) {
430430
*/
431431
async function checkAgreedTerms (userId, terms) {
432432
const unAgreedTerms = []
433+
const missingTerms = []
433434
for (const term of terms) {
434435
const res = await getRequest(`${config.TERMS_API_URL}/${term.id}`, { userId })
435436
if (!_.get(res, 'body.agreed', false)) {
436437
unAgreedTerms.push(_.get(res, 'body.title', term))
438+
missingTerms.push({
439+
termId: term.id,
440+
roleId: term.roleId
441+
})
437442
}
438443
}
439444

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

0 commit comments

Comments
 (0)