2014-11-10 16:27:40 -05:00
|
|
|
log = require 'winston'
|
2015-12-16 20:09:22 -05:00
|
|
|
_ = require 'lodash'
|
2014-11-10 16:27:40 -05:00
|
|
|
|
2014-01-14 17:13:47 -05:00
|
|
|
module.exports.custom = (res, code=500, message='Internal Server Error') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "#{code}: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send code, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.unauthorized = (res, message='Unauthorized') ->
|
|
|
|
# TODO: The response MUST include a WWW-Authenticate header field
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "401: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 401, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.forbidden = (res, message='Forbidden') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "403: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 403, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-12-03 16:32:04 -05:00
|
|
|
|
|
|
|
module.exports.paymentRequired = (res, message='Payment required') ->
|
|
|
|
log.debug "402: #{message}"
|
|
|
|
res.send 402, message
|
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.notFound = (res, message='Not found.') ->
|
|
|
|
res.send 404, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
2014-06-09 11:28:35 -04:00
|
|
|
module.exports.badMethod = (res, allowed=['GET', 'POST', 'PUT', 'PATCH'], message='Method Not Allowed') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "405: #{message}"
|
2014-06-09 11:28:35 -04:00
|
|
|
allowHeader = _.reduce allowed, ((str, current) -> str += ', ' + current)
|
|
|
|
res.set 'Allow', allowHeader # TODO not sure if these are always the case
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 405, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.conflict = (res, message='Conflict. File exists') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "409: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 409, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.badInput = (res, message='Unprocessable Entity. Bad Input.') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "422: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 422, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-01-14 17:13:47 -05:00
|
|
|
|
|
|
|
module.exports.serverError = (res, message='Internal Server Error') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "500: #{message}"
|
2014-01-14 17:13:47 -05:00
|
|
|
res.send 500, message
|
2014-01-17 13:30:47 -05:00
|
|
|
res.end()
|
2014-02-07 15:36:56 -05:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
module.exports.gatewayTimeoutError = (res, message='Gateway timeout') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "504: #{message}"
|
2014-02-07 15:36:56 -05:00
|
|
|
res.send 504, message
|
|
|
|
res.end()
|
2014-02-07 18:52:24 -05:00
|
|
|
|
2014-06-30 22:16:26 -04:00
|
|
|
module.exports.clientTimeout = (res, message='The server did not receive the client response in a timely manner') ->
|
2014-11-10 16:27:40 -05:00
|
|
|
log.debug "408: #{message}"
|
2014-02-08 13:11:43 -05:00
|
|
|
res.send 408, message
|
2014-06-09 11:28:35 -04:00
|
|
|
res.end()
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
|
|
|
|
# Objects
|
|
|
|
|
|
|
|
errorResponseSchema = {
|
|
|
|
type: 'object'
|
|
|
|
required: ['errorName', 'code', 'message']
|
|
|
|
properties: {
|
|
|
|
error: {
|
|
|
|
description: 'Error object which the callback returned'
|
|
|
|
}
|
|
|
|
errorName: {
|
|
|
|
type: 'string'
|
|
|
|
description: 'Human readable error code name'
|
|
|
|
}
|
|
|
|
code: {
|
|
|
|
type: 'integer'
|
|
|
|
description: 'HTTP error code'
|
|
|
|
}
|
|
|
|
validationErrors: {
|
|
|
|
type: 'array'
|
|
|
|
description: 'TV4 array of validation error objects'
|
|
|
|
}
|
|
|
|
message: {
|
|
|
|
type: 'string'
|
|
|
|
description: 'Human readable descripton of the error'
|
|
|
|
}
|
|
|
|
property: {
|
|
|
|
type: 'string'
|
|
|
|
description: 'Property which is related to the error (conflict, validation).'
|
|
|
|
}
|
2016-04-11 19:51:51 -04:00
|
|
|
name: {
|
|
|
|
type: 'string'
|
|
|
|
description: 'Provided for /auth/name.' # TODO: refactor out
|
|
|
|
}
|
2016-06-08 16:45:25 -04:00
|
|
|
errorID: {
|
|
|
|
type: 'string'
|
|
|
|
description: 'Error id to be used by the client to handle specific errors'
|
|
|
|
}
|
2015-12-16 20:09:22 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
errorProps = _.keys(errorResponseSchema.properties)
|
|
|
|
|
2016-07-13 19:50:03 -04:00
|
|
|
class NetworkError extends Error
|
2015-12-16 20:09:22 -05:00
|
|
|
code: 0
|
|
|
|
|
|
|
|
constructor: (@message, options) ->
|
|
|
|
@stack = (new Error()).stack
|
|
|
|
_.assign(@, options)
|
|
|
|
|
|
|
|
toJSON: ->
|
|
|
|
_.pick(@, errorProps...)
|
|
|
|
|
|
|
|
module.exports.NetworkError = NetworkError
|
|
|
|
|
|
|
|
module.exports.Unauthorized = class Unauthorized extends NetworkError
|
|
|
|
code: 401
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Unauthorized'
|
2016-03-30 16:57:19 -04:00
|
|
|
|
|
|
|
module.exports.PaymentRequired = class PaymentRequired extends NetworkError
|
|
|
|
code: 402
|
|
|
|
errorName: 'PaymentRequired'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.Forbidden = class Forbidden extends NetworkError
|
|
|
|
code: 403
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Forbidden'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.NotFound = class NotFound extends NetworkError
|
|
|
|
code: 404
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Not Found'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.MethodNotAllowed = class MethodNotAllowed extends NetworkError
|
|
|
|
code: 405
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Method Not Allowed'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.RequestTimeout = class RequestTimeout extends NetworkError
|
|
|
|
code: 407
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Request Timeout'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.Conflict = class Conflict extends NetworkError
|
|
|
|
code: 409
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Conflict'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.UnprocessableEntity = class UnprocessableEntity extends NetworkError
|
|
|
|
code: 422
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Unprocessable Entity'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.InternalServerError = class InternalServerError extends NetworkError
|
|
|
|
code: 500
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Internal Server Error'
|
2015-12-16 20:09:22 -05:00
|
|
|
|
|
|
|
module.exports.GatewayTimeout = class GatewayTimeout extends NetworkError
|
|
|
|
code: 504
|
2016-03-03 16:59:17 -05:00
|
|
|
errorName: 'Gateway Timeout'
|