@infomaker/service-authorization-lib
Version:
IMID Service Authorization Library
151 lines (138 loc) • 4.49 kB
JavaScript
/*jslint node: true */
const log = require('@infomaker/json-log')(__filename)
const uuid = require('uuid')
/**
* @module Errors
*/
/**
* ServiceAuthorizationError - extending the Error class.
*
* Base class for each kind of errors in ServiceAuthorizationLib.
* @extends Error
*/
class ServiceAuthorizationError extends Error {
/**
* Create a ServiceAuthorizationError error
* @param {Object} args - The args object passed to the constructor
* @param {Object} args.publicData - Data to show in the error
* @param {Object} args.internalData - Private data that can be logged in the error
* @param {string} [args.publicMessage=An error occurred] - Error message to be shown
* @param {number} [args.httpCode=500] - Http status code for error
*/
constructor(args) {
const defaultValues = {
publicMessage: 'An error occurred',
httpCode: 500,
internalData: {},
publicData: {}
}
const fullArgs = Object.assign({}, defaultValues, args)
super(fullArgs.publicMessage)
this.isSALError = true
this.errorId = uuid.v4()
this.httpCode = fullArgs.httpCode
this.internalData = fullArgs.internalData
this.publicMessage = fullArgs.publicMessage
this.publicData = fullArgs.publicData
log.debug('An exception occurred', {
errorId: this.errorId,
error: fullArgs,
stack: this.stack
})
}
/**
* Reply function to respond with the error
* @param {Object} replyOrResponseToolkit - Response toolkit - For example 'h' in Hapi, which handles the response
* @param {Object} [extraPublicData={}] - Extra public data to shown in the error message
* @returns {Object} Response - Response object
*/
hapiReply(replyOrResponseToolkit, extraPublicData={}) {
const replyData = Object.assign({
msg: this.publicMessage,
errorId: this.errorId,
data: this.publicData
}, extraPublicData)
const response = replyOrResponseToolkit.response(replyData)
response.code(this.httpCode)
return response
}
}
/**
* AccessDenied Error - extending the ServiceAuthorizationError error class
*
* PublicMessage is set to 'Access denied' and the HttpCode is set to '403'.
*
* Thrown when a users credentials does not match the requested endpoints credentials.
* @extends ServiceAuthorizationError
*/
class AccessDenied extends ServiceAuthorizationError {
/**
* Create a AccessDenied error
* @param {Object} publicData - Data to show in the error
* @param {Object} internalData - Private data that can be logged in the error
*/
constructor(internalData, publicData) {
const defaultValues = {
publicMessage: 'Access denied',
httpCode: 403,
internalData: {},
publicData: {}
}
super(Object.assign({}, defaultValues, {internalData, publicData}))
}
}
/**
* Unauthorized - extending the ServiceAuthorizationError error class
*
* PublicMessage is set to 'Unauthorized' and the HttpCode is set to '401'.
*
* Thrown when a user tries to request an endpoint with no access to it.
* @extends ServiceAuthorizationError
*/
class Unauthorized extends ServiceAuthorizationError {
/**
* Create a Unauthorized error
* @param {Object} publicData - Data to show in the error
* @param {Object} internalData - Private data that can be logged in the error
*/
constructor(internalData, publicData) {
const defaultValues = {
publicMessage: 'Unauthorized',
httpCode: 401,
internalData: {},
publicData: {}
}
super(Object.assign({}, defaultValues, {internalData, publicData}))
}
}
/**
* ConfigError - extending the ServiceAuthorizationError error class
*
* PublicMessage is set to 'Internal Server Error' and the HttpCode is set to '500'.
*
* Thrown when building config variables while authorize.
* @extends ServiceAuthorizationError
*/
class ConfigError extends ServiceAuthorizationError {
/**
* Create a ConfigError error
* @param {Object} publicData - Data to show in the error
* @param {Object} internalData - Private data that can be logged in the error
*/
constructor(internalData, publicData) {
const defaultValues = {
publicMessage: 'Internal Server Error',
httpCode: 500,
internalData: {},
publicData: {}
}
super(Object.assign({}, defaultValues, {internalData, publicData}))
}
}
module.exports = {
ServiceAuthorizationError,
AccessDenied,
Unauthorized,
ConfigError
}