Skip to content

Commit

Permalink
Merge pull request #11 from beyonk-adventures/feature/add-402-error
Browse files Browse the repository at this point in the history
Adds 402 payment required error handler
  • Loading branch information
stuplum authored Feb 2, 2021
2 parents 64507a2 + 64b26a1 commit a7ea93f
Show file tree
Hide file tree
Showing 5 changed files with 3,427 additions and 1 deletion.
6 changes: 6 additions & 0 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ console.log(profile)
.forbidden(e => {
console.error('Forbidden', e)
})
.gone(e => {
console.error('Gone', e)
})
.notFound(e => {
console.error('Not found', e)
})
Expand All @@ -157,6 +160,9 @@ console.log(profile)
.conflict(e => {
console.error('Conflict', e)
})
.paymentRequired(e => {
console.error('Payment Required', e)
})
.preconditionFailed(e => {
console.error('Precondition failed', e)
})
Expand Down
2 changes: 2 additions & 0 deletions lib/error-handlers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { ForbiddenMixin } = require('./forbidden')
const { HandleMixin } = require('./handle')
const { NotFoundMixin } = require('./not-found')
const { BadDataMixin } = require('./bad-data')
const { PaymentRequiredMixin } = require('./payment-required')
const { PreconditionFailedMixin } = require('./precondition-failed')
const { TooManyRequestsMixin } = require('./too-many-requests')
const { NotAcceptableMixin } = require('./not-acceptable')
Expand All @@ -20,6 +21,7 @@ module.exports = {
HandleMixin,
NotFoundMixin,
BadDataMixin,
PaymentRequiredMixin,
PreconditionFailedMixin,
TooManyRequestsMixin,
NotAcceptableMixin,
Expand Down
12 changes: 12 additions & 0 deletions lib/error-handlers/payment-required.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'

const {
PaymentRequiredError
} = require('../errors')

exports.PaymentRequiredMixin = superclass => class extends superclass {
paymentRequired (fn) {
this.handlers[PaymentRequiredError.name] = fn
return this
}
}
5 changes: 5 additions & 0 deletions lib/errors/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ class HttpError extends Error {
class AccessDeniedError extends HttpError {
}

class PaymentRequiredError extends HttpError {
}

class ForbiddenError extends HttpError {
}

Expand All @@ -36,6 +39,7 @@ class TooManyRequestsError extends HttpError {

const mapping = {
401: AccessDeniedError,
402: PaymentRequiredError,
403: ForbiddenError,
404: NotFoundError,
406: NotAcceptableError,
Expand All @@ -53,6 +57,7 @@ function byCode (code) {
module.exports = {
byCode,
AccessDeniedError,
PaymentRequiredError,
ForbiddenError,
NotFoundError,
NotAcceptableError,
Expand Down
Loading

0 comments on commit a7ea93f

Please sign in to comment.