Skip to content

Commit 4b0f48c

Browse files
Augment error object with metadata and Update Terms Error Response
1 parent bbcc913 commit 4b0f48c

File tree

4 files changed

+35
-4
lines changed

4 files changed

+35
-4
lines changed

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: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -427,18 +427,26 @@ function partialMatch (filter, value) {
427427
* Check if the user has agreed to all challenge terms
428428
* @param {Number} userId the user ID
429429
* @param {Array<String>} terms an array of term UUIDs to check
430+
* @param {String} roleId the role ID
430431
*/
431-
async function checkAgreedTerms (userId, terms) {
432+
async function checkAgreedTerms (userId, terms, roleId) {
432433
const unAgreedTerms = []
434+
const unAgreedTermIds = []
433435
for (const term of terms) {
434436
const res = await getRequest(`${config.TERMS_API_URL}/${term.id}`, { userId })
435437
if (!_.get(res, 'body.agreed', false)) {
436438
unAgreedTerms.push(_.get(res, 'body.title', term))
439+
unAgreedTermIds.push({
440+
termId: term.id,
441+
roleId
442+
})
437443
}
438444
}
439445

440446
if (unAgreedTerms.length > 0) {
441-
throw new errors.ForbiddenError(`The user has not yet agreed to the following terms: [${unAgreedTerms.join(', ')}]`)
447+
throw new errors.ForbiddenError(`The user has not yet agreed to the following terms: [${unAgreedTerms.join(', ')}]`, null, {
448+
missingTerms: unAgreedTermIds
449+
})
442450
}
443451
}
444452

src/services/ResourceService.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ async function init (currentUser, challengeId, resource, isCreated) {
252252
let resources
253253
// Verify the member has agreed to the challenge terms
254254
if (isCreated) {
255-
await helper.checkAgreedTerms(memberId, _.filter(_.get(challenge, 'terms', []), t => t.roleId === resourceRole.id))
255+
await helper.checkAgreedTerms(memberId, _.filter(_.get(challenge, 'terms', []), t => t.roleId === resourceRole.id), resourceRole.id)
256256
}
257257
if (!currentUser.isMachine && !helper.hasAdminRole(currentUser)) {
258258
// Check if user has agreed to the challenge terms

0 commit comments

Comments
 (0)