All files / server/api/utils errors.js

95.45% Statements 21/22
80% Branches 8/10
87.5% Functions 7/8
95.45% Lines 21/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110          1x                                   18x   18x 18x 18x 18x     1x   1x                     1x 10x             1x 5x             1x               1x 2x                           1x 3x                           1x 5x             1x 5x    
/**
 * Reusable API errors.
 *
 * @module server/api/utils/errors
 */
const util = require('util');
 
/**
 * An API error.
 *
 * When thrown from an API route or middleware, an error of this type will
 * automatically be serialized as JSON and the HTTP response will have the
 * correct status code.
 *
 * @class
 * @extends Error
 * @property {string} name - The type of error.
 * @property {number} status - The HTTP status code to respond with when this error occurs.
 * @property {string} code - A code identifying the error (e.g. `category.whatWentWrong`).
 * @property {string} message - A description of the problem.
 */
function ApiError(status, code, message) {
 
  Error.captureStackTrace(this, this.constructor);
 
  this.name = this.constructor.name;
  this.status = status;
  this.code = code;
  this.message = message;
}
 
util.inherits(ApiError, Error);
 
exports.ApiError = ApiError;
 
/**
 * Returns an HTTP 401 Unauthorized error.
 *
 * @param {string} code - A code identifying the error (e.g. `auth.whatWentWrong`).
 *
 * @param {string} message - A description of the problem.
 *
 * @returns {ApiError} An API error.
 */
exports.unauthorized = function(code, message) {
  return new ApiError(401, code, message || 'Authentication is required to access this resource. Authenticate by providing a Bearer token in the Authorization header.');
};
 
/**
 * Returns an HTTP 401 Unauthorized error with the `auth.missingAuthorization` code due to missing credentials.
 * @returns {ApiError} An API error.
 */
exports.missingAuthorization = function() {
  return exports.unauthorized('auth.missingAuthorization');
};
 
/**
 * Returns an HTTP 401 Unauthorized error with the `auth.malformedAuthorization` code due to malformed credentials (e.g. a badly formatted Authorization header).
 * @returns {ApiError} An API error.
 */
exports.malformedAuthorization = function() {
  return exports.unauthorized('auth.malformedAuthorization', 'The Authorization header is not in the correct format. It should be "Authorization: Bearer TOKEN".');
};
 
/**
 * Returns an HTTP 401 Unauthorized error with the `auth.invalidAuthorization` code due to invalid credentials (e.g. an expired JWT).
 * @returns {ApiError} An API error.
 */
exports.invalidAuthorization = function() {
  return exports.unauthorized('auth.invalidAuthorization', 'The Bearer token supplied in the Authorization header is invalid or has expired.');
};
 
/**
 * Returns an HTTP 403 Forbidden error.
 *
 * The message defaults to "You are not authorized to access this resource. Authenticate with a user account that has more privileges.".
 *
 * @param {string} [code=auth.forbidden] - A code identifying the error.
 *
 * @param {string} [message] - A description of the problem.
 *
 * @returns {ApiError} An API error.
 */
exports.forbidden = function(code, message) {
  return new ApiError(403, code || 'auth.forbidden', message || 'You are not authorized to access this resource. Authenticate with a user account that has more privileges.');
};
 
/**
 * Returns an HTTP 404 Not Found error.
 *
 * The message defaults to "No resource was found at this verb and URI.".
 *
 * @param {string} [code=resource.notFound] - A code identifying the error.
 *
 * @param {string} [message] - A description of the problem.
 *
 * @returns {ApiError} An API error.
 */
exports.notFound = function(code, message) {
  return new ApiError(404, code || 'resource.notFound', message || 'No resource was found at this verb and URI.');
};
 
/**
 * Returns an HTTP 404 Not Found error due to a missing resource.
 * @returns {ApiError} An API error.
 */
exports.recordNotFound = function(name, id) {
  return exports.notFound('record.notFound', 'No ' + name + ' was found with ID ' + id + '.');
};