Comparing version 1.0.28 to 1.0.29
{ | ||
"name": "grpc-boom", | ||
"version": "1.0.28", | ||
"version": "1.0.29", | ||
"description": "A gRPC implementation of the awesome Boom library to help create gRPC-friendly error objects", | ||
@@ -32,15 +32,15 @@ "main": "src/index.js", | ||
"devDependencies": { | ||
"@types/jest": "^25.2.3", | ||
"@types/node": "^14.0.6", | ||
"grpc": "^1.24.2", | ||
"jest": "^26.0.1", | ||
"@types/jest": "^26.0.24", | ||
"@types/node": "^16.4.13", | ||
"@grpc/grpc-js": "^1.3.6", | ||
"jest": "^27.0.6", | ||
"jest-coverage-badges": "^1.1.2", | ||
"npm-check-updates": "^6.0.1", | ||
"prettier": "^2.0.5", | ||
"protobufjs": "^6.9.0", | ||
"ts-jest": "^26.0.0", | ||
"ts-node": "^8.10.2", | ||
"tslint": "^6.1.2", | ||
"typescript": "^3.9.3" | ||
"npm-check-updates": "^11.8.3", | ||
"prettier": "^2.3.2", | ||
"protobufjs": "^6.11.2", | ||
"ts-jest": "^27.0.4", | ||
"ts-node": "^10.1.0", | ||
"tslint": "^6.1.3", | ||
"typescript": "^4.3.5" | ||
} | ||
} |
@@ -7,3 +7,3 @@ # gRPC Boom | ||
 | ||
[](https://travis-ci.com/nicolaspearson/grpc.boom) | ||
 | ||
@@ -15,3 +15,3 @@ [license-url]: https://opensource.org/licenses/BSD-3-Clause | ||
This library has **zero** external dependencies, but it is assumed that you are using the `grpc` library. | ||
This library has **zero** external dependencies, but it is assumed that you are using the `@grpc/grpc-js` library. | ||
@@ -24,6 +24,6 @@ ## Installation | ||
Install the `grpc` library: | ||
Install the `@grpc/grpc-js` library: | ||
``` | ||
npm install grpc --save | ||
npm install @grpc/grpc-js --save | ||
``` | ||
@@ -30,0 +30,0 @@ |
247
src/index.js
@@ -24,139 +24,136 @@ "use strict"; | ||
})(Status = exports.Status || (exports.Status = {})); | ||
let GrpcBoom = (() => { | ||
class GrpcBoom extends Error { | ||
constructor(message, options) { | ||
super(message); | ||
const code = options && options.code !== undefined ? options.code : GrpcBoom.fallbackStatus; | ||
const ctor = options && options.ctor !== undefined ? options.ctor : GrpcBoom; | ||
const error = options && options.error !== undefined ? options.error : undefined; | ||
const errorInstance = new Error(message !== undefined ? message : undefined); | ||
errorInstance.isBoom = true; | ||
errorInstance.code = code; | ||
errorInstance.error = error; | ||
errorInstance.message = message; | ||
errorInstance.reformat = this.reformat; | ||
errorInstance.initialize = this.initialize; | ||
if (options && options.metadata !== undefined) { | ||
errorInstance.metadata = options.metadata; | ||
} | ||
errorInstance.reformat(); | ||
Error.captureStackTrace(errorInstance, ctor); | ||
return errorInstance; | ||
class GrpcBoom extends Error { | ||
constructor(message, options) { | ||
super(message); | ||
const code = options && options.code !== undefined ? options.code : GrpcBoom.fallbackStatus; | ||
const ctor = options && options.ctor !== undefined ? options.ctor : GrpcBoom; | ||
const error = options && options.error !== undefined ? options.error : undefined; | ||
const errorInstance = new Error(message !== undefined ? message : undefined); | ||
errorInstance.isBoom = true; | ||
errorInstance.code = code; | ||
errorInstance.error = error; | ||
errorInstance.message = message; | ||
errorInstance.reformat = this.reformat; | ||
errorInstance.initialize = this.initialize; | ||
if (options && options.metadata !== undefined) { | ||
errorInstance.metadata = options.metadata; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
return instance && instance.isBoom; | ||
errorInstance.reformat(); | ||
Error.captureStackTrace(errorInstance, ctor); | ||
return errorInstance; | ||
} | ||
static [Symbol.hasInstance](instance) { | ||
return instance && instance.isBoom; | ||
} | ||
static boomify(errorInstance, options) { | ||
let message = errorInstance && errorInstance.message ? errorInstance.message : GrpcBoom.fallbackMessage; | ||
if (options && options.message && !(options.message instanceof Error)) { | ||
message = options.message; | ||
} | ||
static boomify(errorInstance, options) { | ||
let message = errorInstance && errorInstance.message ? errorInstance.message : GrpcBoom.fallbackMessage; | ||
if (options && options.message && !(options.message instanceof Error)) { | ||
message = options.message; | ||
} | ||
let code = errorInstance && errorInstance.code ? errorInstance.code : GrpcBoom.fallbackStatus; | ||
if (options && options.code) { | ||
code = options.code; | ||
} | ||
let error; | ||
if (options && options.error) { | ||
error = options.error; | ||
} | ||
if (errorInstance && errorInstance.isBoom) { | ||
errorInstance.message = message; | ||
errorInstance.code = code; | ||
errorInstance.error = error; | ||
return errorInstance; | ||
} | ||
const newOptions = { code, error }; | ||
if (options && options.metadata) { | ||
newOptions.metadata = options.metadata; | ||
} | ||
return new GrpcBoom(message, newOptions); | ||
let code = errorInstance && errorInstance.code ? errorInstance.code : GrpcBoom.fallbackStatus; | ||
if (options && options.code) { | ||
code = options.code; | ||
} | ||
static ok(message, metadata) { | ||
return this.create(message, Status.OK, metadata, this.ok); | ||
let error; | ||
if (options && options.error) { | ||
error = options.error; | ||
} | ||
static cancelled(message, metadata) { | ||
return this.create(message, Status.CANCELLED, metadata, this.cancelled); | ||
if (errorInstance && errorInstance.isBoom) { | ||
errorInstance.message = message; | ||
errorInstance.code = code; | ||
errorInstance.error = error; | ||
return errorInstance; | ||
} | ||
static unknown(message, metadata) { | ||
return this.create(message, Status.UNKNOWN, metadata, this.unknown); | ||
const newOptions = { code, error }; | ||
if (options && options.metadata) { | ||
newOptions.metadata = options.metadata; | ||
} | ||
static invalidArgument(message, metadata) { | ||
return this.create(message, Status.INVALID_ARGUMENT, metadata, this.invalidArgument); | ||
return new GrpcBoom(message, newOptions); | ||
} | ||
static ok(message, metadata) { | ||
return this.create(message, Status.OK, metadata, this.ok); | ||
} | ||
static cancelled(message, metadata) { | ||
return this.create(message, Status.CANCELLED, metadata, this.cancelled); | ||
} | ||
static unknown(message, metadata) { | ||
return this.create(message, Status.UNKNOWN, metadata, this.unknown); | ||
} | ||
static invalidArgument(message, metadata) { | ||
return this.create(message, Status.INVALID_ARGUMENT, metadata, this.invalidArgument); | ||
} | ||
static deadlineExceeded(message, metadata) { | ||
return this.create(message, Status.DEADLINE_EXCEEDED, metadata, this.deadlineExceeded); | ||
} | ||
static notFound(message, metadata) { | ||
return this.create(message, Status.NOT_FOUND, metadata, this.notFound); | ||
} | ||
static alreadyExists(message, metadata) { | ||
return this.create(message, Status.ALREADY_EXISTS, metadata, this.alreadyExists); | ||
} | ||
static permissionDenied(message, metadata) { | ||
return this.create(message, Status.PERMISSION_DENIED, metadata, this.permissionDenied); | ||
} | ||
static resourceExhausted(message, metadata) { | ||
return this.create(message, Status.RESOURCE_EXHAUSTED, metadata, this.resourceExhausted); | ||
} | ||
static failedPrecondition(message, metadata) { | ||
return this.create(message, Status.FAILED_PRECONDITION, metadata, this.failedPrecondition); | ||
} | ||
static aborted(message, metadata) { | ||
return this.create(message, Status.ABORTED, metadata, this.aborted); | ||
} | ||
static outOfRange(message, metadata) { | ||
return this.create(message, Status.OUT_OF_RANGE, metadata, this.outOfRange); | ||
} | ||
static unimplemented(message, metadata) { | ||
return this.create(message, Status.UNIMPLEMENTED, metadata, this.unimplemented); | ||
} | ||
static internal(message, metadata) { | ||
return this.create(message, Status.INTERNAL, metadata, this.internal); | ||
} | ||
static unavailable(message, metadata) { | ||
return this.create(message, Status.UNAVAILABLE, metadata, this.unavailable); | ||
} | ||
static dataLoss(message, metadata) { | ||
return this.create(message, Status.DATA_LOSS, metadata, this.dataLoss); | ||
} | ||
static unauthenticated(message, metadata) { | ||
return this.create(message, Status.UNAUTHENTICATED, metadata, this.unauthenticated); | ||
} | ||
static create(message, code, metadata, ctor) { | ||
const grpcBoom = new GrpcBoom(message, { | ||
code, | ||
metadata, | ||
ctor, | ||
}); | ||
return grpcBoom.initialize(grpcBoom, code, message, metadata); | ||
} | ||
initialize(errorInstance, code, message, metadata) { | ||
this.isBoom = true; | ||
if (metadata) { | ||
this.metadata = metadata; | ||
} | ||
static deadlineExceeded(message, metadata) { | ||
return this.create(message, Status.DEADLINE_EXCEEDED, metadata, this.deadlineExceeded); | ||
this.code = code; | ||
if (message === undefined && errorInstance.message === undefined) { | ||
this.reformat(); | ||
message = this.error; | ||
} | ||
static notFound(message, metadata) { | ||
return this.create(message, Status.NOT_FOUND, metadata, this.notFound); | ||
this.reformat(); | ||
return this; | ||
} | ||
reformat(debug) { | ||
if (this.code === undefined) { | ||
this.code = GrpcBoom.fallbackStatus; | ||
} | ||
static alreadyExists(message, metadata) { | ||
return this.create(message, Status.ALREADY_EXISTS, metadata, this.alreadyExists); | ||
if (this.error === undefined) { | ||
this.error = Status[this.code]; | ||
} | ||
static permissionDenied(message, metadata) { | ||
return this.create(message, Status.PERMISSION_DENIED, metadata, this.permissionDenied); | ||
if (this.message === undefined) { | ||
this.message = GrpcBoom.fallbackMessage; | ||
} | ||
static resourceExhausted(message, metadata) { | ||
return this.create(message, Status.RESOURCE_EXHAUSTED, metadata, this.resourceExhausted); | ||
} | ||
static failedPrecondition(message, metadata) { | ||
return this.create(message, Status.FAILED_PRECONDITION, metadata, this.failedPrecondition); | ||
} | ||
static aborted(message, metadata) { | ||
return this.create(message, Status.ABORTED, metadata, this.aborted); | ||
} | ||
static outOfRange(message, metadata) { | ||
return this.create(message, Status.OUT_OF_RANGE, metadata, this.outOfRange); | ||
} | ||
static unimplemented(message, metadata) { | ||
return this.create(message, Status.UNIMPLEMENTED, metadata, this.unimplemented); | ||
} | ||
static internal(message, metadata) { | ||
return this.create(message, Status.INTERNAL, metadata, this.internal); | ||
} | ||
static unavailable(message, metadata) { | ||
return this.create(message, Status.UNAVAILABLE, metadata, this.unavailable); | ||
} | ||
static dataLoss(message, metadata) { | ||
return this.create(message, Status.DATA_LOSS, metadata, this.dataLoss); | ||
} | ||
static unauthenticated(message, metadata) { | ||
return this.create(message, Status.UNAUTHENTICATED, metadata, this.unauthenticated); | ||
} | ||
static create(message, code, metadata, ctor) { | ||
const grpcBoom = new GrpcBoom(message, { | ||
code, | ||
metadata, | ||
ctor | ||
}); | ||
return grpcBoom.initialize(grpcBoom, code, message, metadata); | ||
} | ||
initialize(errorInstance, code, message, metadata) { | ||
this.isBoom = true; | ||
if (metadata) { | ||
this.metadata = metadata; | ||
} | ||
this.code = code; | ||
if (message === undefined && errorInstance.message === undefined) { | ||
this.reformat(); | ||
message = this.error; | ||
} | ||
this.reformat(); | ||
return this; | ||
} | ||
reformat(debug) { | ||
if (this.code === undefined) { | ||
this.code = GrpcBoom.fallbackStatus; | ||
} | ||
if (this.error === undefined) { | ||
this.error = Status[this.code]; | ||
} | ||
if (this.message === undefined) { | ||
this.message = GrpcBoom.fallbackMessage; | ||
} | ||
} | ||
} | ||
GrpcBoom.fallbackStatus = Status.UNKNOWN; | ||
GrpcBoom.fallbackMessage = 'An unknown error occurred'; | ||
return GrpcBoom; | ||
})(); | ||
} | ||
exports.default = GrpcBoom; | ||
GrpcBoom.fallbackStatus = Status.UNKNOWN; | ||
GrpcBoom.fallbackMessage = 'An unknown error occurred'; |
38049
491