@blossm/errors
Advanced tools
Comparing version 0.0.29 to 0.0.30
71
index.js
@@ -1,11 +0,64 @@ | ||
const badRequest = require("./src/bad_request"); | ||
const internalServer = require("./src/internal_server"); | ||
const unauthorized = require("./src/unauthorized"); | ||
const resourceNotFound = require("./src/resource_not_found"); | ||
const invalidCredentials = require("./src/invalid_credentials"); | ||
const forbidden = require("./src/forbidden"); | ||
const invalidArgument = require("./src/invalid_argument"); | ||
const construct = require("./src/construct"); | ||
const preconditionFailed = require("./src/precondition_failed"); | ||
const { | ||
BadRequestError, | ||
InternalServerError, | ||
UnauthorizedError, | ||
ResourceNotFoundError, | ||
InvalidArgumentError, | ||
InvalidCredentialsError, | ||
ForbiddenError, | ||
PreconditionFailedError, | ||
} = require("restify-errors"); | ||
const toJSON = require("./src/to_json"); | ||
const badRequest = { | ||
message: (message, { cause, info } = {}) => | ||
new BadRequestError({ cause, info, toJSON }, message), | ||
}; | ||
const internalServer = { | ||
message: (message, { cause, info } = {}) => | ||
new InternalServerError({ cause, info, toJSON }, message), | ||
}; | ||
const unauthorized = { | ||
message: (message, { cause, info } = {}) => | ||
new UnauthorizedError({ cause, info, toJSON }, message), | ||
}; | ||
const resourceNotFound = { | ||
message: (message, { cause, info } = {}) => | ||
new ResourceNotFoundError({ cause, info, toJSON }, message), | ||
}; | ||
const invalidCredentials = { | ||
message: (message, { cause, info } = {}) => | ||
new InvalidCredentialsError({ cause, info, toJSON }, message), | ||
}; | ||
const forbidden = { | ||
message: (message, { cause, info } = {}) => | ||
new ForbiddenError({ cause, info, toJSON }, message), | ||
}; | ||
const invalidArgument = { | ||
message: (message, { cause, info } = {}) => | ||
new InvalidArgumentError({ cause, info, toJSON }, message), | ||
}; | ||
const preconditionFailed = { | ||
message: (message, { cause, info } = {}) => | ||
new PreconditionFailedError({ cause, info, toJSON }, message), | ||
}; | ||
const construct = ({ statusCode, message }) => { | ||
switch (statusCode) { | ||
case 400: | ||
return badRequest.message(message); | ||
case 401: | ||
return unauthorized.message(message); | ||
case 403: | ||
return forbidden.message(message); | ||
case 404: | ||
return resourceNotFound.message(message); | ||
case 409: | ||
return invalidArgument.message(message); | ||
case 500: | ||
return internalServer.message(message); | ||
} | ||
}; | ||
module.exports = { | ||
@@ -12,0 +65,0 @@ badRequest, |
{ | ||
"name": "@blossm/errors", | ||
"version": "0.0.29", | ||
"version": "0.0.30", | ||
"description": "A library of errors that can be thrown by the network.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,14 +8,2 @@ const { expect } = require("chai"); | ||
describe("Bad request", () => { | ||
it("missingRoot correct", () => { | ||
const error = badRequest.missingRoot(); | ||
expect(error.message).to.equal("Missing root url parameter."); | ||
expect(error.statusCode).to.equal(400); | ||
}); | ||
it("missingRoot correct with props", () => { | ||
const error = badRequest.missingRoot({ cause, info }); | ||
expect(error.message).to.equal("Missing root url parameter."); | ||
expect(error.toJSON().info).to.deep.equal(info); | ||
expect(error.cause()).to.deep.equal(cause); | ||
expect(error.statusCode).to.equal(400); | ||
}); | ||
it("message correct", () => { | ||
@@ -22,0 +10,0 @@ const message = "some-message"; |
@@ -8,14 +8,2 @@ const { expect } = require("chai"); | ||
describe("Forbidden", () => { | ||
it("wrongContext correct", () => { | ||
const error = forbidden.wrongContext(); | ||
expect(error.message).to.equal("Missing required permissions."); | ||
expect(error.statusCode).to.equal(403); | ||
}); | ||
it("missingRoot correct with props", () => { | ||
const error = forbidden.wrongContext({ cause, info }); | ||
expect(error.message).to.equal("Missing required permissions."); | ||
expect(error.toJSON().info).to.deep.equal(info); | ||
expect(error.cause()).to.deep.equal(cause); | ||
expect(error.statusCode).to.equal(403); | ||
}); | ||
it("message correct", () => { | ||
@@ -22,0 +10,0 @@ const message = "some-message"; |
@@ -8,90 +8,2 @@ const { expect } = require("chai"); | ||
describe("Invalid credentials", () => { | ||
it("tokenInvalid correct", () => { | ||
const error = invalidCredentials.tokenInvalid(); | ||
expect(error.message).to.equal("This token is invalid."); | ||
expect(error.statusCode).to.equal(401); | ||
}); | ||
it("tokenInvalid correct with props", () => { | ||
const error = invalidCredentials.tokenInvalid({ cause, info }); | ||
expect(error.message).to.equal("This token is invalid."); | ||
expect(error.info()).to.deep.equal(info); | ||
expect(error.cause()).to.deep.equal(cause); | ||
expect(error.statusCode).to.equal(401); | ||
}); | ||
it("tokenExpired correct", () => { | ||
const error = invalidCredentials.tokenExpired(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info: { reason: "expired" }, | ||
message: "This token is expired.", | ||
}); | ||
}); | ||
it("tokenExpired correct with props", () => { | ||
const error = invalidCredentials.tokenExpired({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info: { ...info, reason: "expired" }, | ||
cause, | ||
message: "This token is expired.", | ||
}); | ||
}); | ||
it("wrongAudience correct", () => { | ||
const error = invalidCredentials.wrongAudience(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info: {}, | ||
message: "This token isn't meant for this audience.", | ||
}); | ||
}); | ||
it("wrongAudience correct with props", () => { | ||
const error = invalidCredentials.wrongAudience({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info, | ||
cause, | ||
message: "This token isn't meant for this audience.", | ||
}); | ||
}); | ||
it("tokenNotActive correct", () => { | ||
const error = invalidCredentials.tokenNotActive(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info: {}, | ||
message: "This token is not active.", | ||
}); | ||
}); | ||
it("tokenNotActive correct with props", () => { | ||
const error = invalidCredentials.tokenNotActive({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info, | ||
cause, | ||
message: "This token is not active.", | ||
}); | ||
}); | ||
it("tokenTerminated correct", () => { | ||
const error = invalidCredentials.tokenTerminated(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info: {}, | ||
message: "This token has already been terminated.", | ||
}); | ||
}); | ||
it("tokenTerminated correct with props", () => { | ||
const error = invalidCredentials.tokenTerminated({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "InvalidCredentials", | ||
info, | ||
cause, | ||
message: "This token has already been terminated.", | ||
}); | ||
}); | ||
it("message correct", () => { | ||
@@ -98,0 +10,0 @@ const message = "some-message"; |
@@ -8,26 +8,2 @@ const { expect } = require("chai"); | ||
describe("Precondition failed", () => { | ||
it("eventNumberIncorrect correct", () => { | ||
const error = preconditionFailed.eventNumberIncorrect(); | ||
expect(error.message).to.equal("Event number incorrect."); | ||
expect(error.statusCode).to.equal(412); | ||
}); | ||
it("eventNumberIncorrect correct with props", () => { | ||
const error = preconditionFailed.eventNumberIncorrect({ cause, info }); | ||
expect(error.message).to.equal("Event number incorrect."); | ||
expect(error.toJSON().info).to.deep.equal(info); | ||
expect(error.cause()).to.deep.equal(cause); | ||
expect(error.statusCode).to.equal(412); | ||
}); | ||
it("eventNumberDuplicate correct", () => { | ||
const error = preconditionFailed.eventNumberDuplicate(); | ||
expect(error.message).to.equal("Event number duplicate."); | ||
expect(error.statusCode).to.equal(412); | ||
}); | ||
it("eventNumberDuplicate correct with props", () => { | ||
const error = preconditionFailed.eventNumberDuplicate({ cause, info }); | ||
expect(error.message).to.equal("Event number duplicate."); | ||
expect(error.toJSON().info).to.deep.equal(info); | ||
expect(error.cause()).to.deep.equal(cause); | ||
expect(error.statusCode).to.equal(412); | ||
}); | ||
it("message correct", () => { | ||
@@ -34,0 +10,0 @@ const message = "some-message"; |
@@ -8,42 +8,2 @@ const { expect } = require("chai"); | ||
describe("Bad request", () => { | ||
it("root correct", () => { | ||
const error = resourceNotFound.root(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 404, | ||
code: "ResourceNotFound", | ||
info: {}, | ||
message: "This root wasn't found.", | ||
}); | ||
}); | ||
it("root correct with props", () => { | ||
const error = resourceNotFound.root({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 404, | ||
code: "ResourceNotFound", | ||
info, | ||
cause, | ||
message: "This root wasn't found.", | ||
}); | ||
}); | ||
it("view id correct", () => { | ||
const error = resourceNotFound.view(); | ||
expect(error.message).to.equal("This view wasn't found."); | ||
expect(error.statusCode).to.equal(404); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 404, | ||
code: "ResourceNotFound", | ||
info: {}, | ||
message: "This view wasn't found.", | ||
}); | ||
}); | ||
it("id correct with props", () => { | ||
const error = resourceNotFound.view({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 404, | ||
code: "ResourceNotFound", | ||
info, | ||
cause, | ||
message: "This view wasn't found.", | ||
}); | ||
}); | ||
it("message correct", () => { | ||
@@ -50,0 +10,0 @@ const message = "some-message"; |
@@ -8,21 +8,2 @@ const { expect } = require("chai"); | ||
describe("Unauthorized", () => { | ||
it("cors correct", () => { | ||
const error = unauthorized.cors(); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "Unauthorized", | ||
info: {}, | ||
message: "Not allowed by CORS.", | ||
}); | ||
}); | ||
it("cors correct with props", () => { | ||
const error = unauthorized.cors({ cause, info }); | ||
expect(error.toJSON()).to.deep.equal({ | ||
statusCode: 401, | ||
code: "Unauthorized", | ||
info, | ||
cause, | ||
message: "Not allowed by CORS.", | ||
}); | ||
}); | ||
it("message correct", () => { | ||
@@ -29,0 +10,0 @@ const message = "some-message"; |
10106
13
355