feat(api-errors): error factories (#1)
This commit is contained in:
parent
0d6b7d6f04
commit
e7b739bb3b
@ -25,7 +25,8 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"babel-runtime": "^6.18.0",
|
"babel-runtime": "^6.18.0",
|
||||||
"lodash": "^4.16.6"
|
"lodash": "^4.16.6",
|
||||||
|
"make-error": "^1.2.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"babel-cli": "^6.18.0",
|
"babel-cli": "^6.18.0",
|
||||||
|
@ -0,0 +1,79 @@
|
|||||||
|
import { BaseError } from 'make-error'
|
||||||
|
import { isArray, iteratee } from 'lodash'
|
||||||
|
|
||||||
|
class XoError extends BaseError {
|
||||||
|
constructor ({ code, message, data }) {
|
||||||
|
super(message)
|
||||||
|
this.code = code
|
||||||
|
this.data = data
|
||||||
|
}
|
||||||
|
|
||||||
|
toJsonRpcError () {
|
||||||
|
return {
|
||||||
|
message: this.message,
|
||||||
|
code: this.code,
|
||||||
|
data: this.data
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const create = (code, getProps) => {
|
||||||
|
const factory = args => new XoError({ ...getProps(args), code })
|
||||||
|
factory.is = (error, predicate) =>
|
||||||
|
error.code === code && iteratee(predicate)(error)
|
||||||
|
|
||||||
|
return factory
|
||||||
|
}
|
||||||
|
|
||||||
|
// =============================================================================
|
||||||
|
|
||||||
|
export const notImplemented = create(0, () => ({
|
||||||
|
message: 'not implemented'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const noSuchObject = create(1, (id, type) => ({
|
||||||
|
data: { id, type },
|
||||||
|
message: 'no such object'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const unauthorized = create(2, () => ({
|
||||||
|
message: 'not authenticated or not enough permissions'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const invalidCredentials = create(3, () => ({
|
||||||
|
message: 'invalid credentials'
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Deprecated alreadyAuthenticated (4)
|
||||||
|
|
||||||
|
export const forbiddenOperation = create(5, (operation, reason) => ({
|
||||||
|
data: { operation, reason },
|
||||||
|
message: `forbidden operation: ${operation}`
|
||||||
|
}))
|
||||||
|
|
||||||
|
// Deprecated GenericError (6)
|
||||||
|
|
||||||
|
export const noHostsAvailable = create(7, () => ({
|
||||||
|
message: 'no hosts available'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const authenticationFailed = create(8, () => ({
|
||||||
|
message: 'authentication failed'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const hostUnreached = create(9, id => ({
|
||||||
|
data: { id },
|
||||||
|
message: 'host unreached'
|
||||||
|
}))
|
||||||
|
|
||||||
|
export const invalidParameters = create(10, (message, errors) => {
|
||||||
|
if (isArray(message)) {
|
||||||
|
errors = message
|
||||||
|
message = undefined
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
data: { errors },
|
||||||
|
message: message || 'invalid parameters'
|
||||||
|
}
|
||||||
|
})
|
Loading…
Reference in New Issue
Block a user