Compare commits
4 Commits
developmen
...
ft/refacto
Author | SHA1 | Date |
---|---|---|
David Pineau | 44c18c1423 | |
Antonin Coulibaly | 9adc02a6ba | |
Antonin Coulibaly | 12c5d362c6 | |
Antonin Coulibaly | ea4eb6a4fb |
16
README.md
16
README.md
|
@ -35,6 +35,8 @@ console.log(array);
|
|||
|
||||
### Usage
|
||||
|
||||
#### Get an error
|
||||
|
||||
``` js
|
||||
import { errors } from 'arsenal';
|
||||
|
||||
|
@ -46,3 +48,17 @@ console.log(errors.AccessDenied);
|
|||
// AccessDenied: true }
|
||||
|
||||
```
|
||||
|
||||
#### Map an error
|
||||
|
||||
``` js
|
||||
import { errors } from 'arsenal';
|
||||
|
||||
console.log(errors.DBNotFound.errorsMap());
|
||||
|
||||
// { [Error: NoSuchBucket]
|
||||
// code: 404,
|
||||
// description: 'The specified bucket does not exist.',
|
||||
// NoSuchBucket: true }
|
||||
|
||||
```
|
||||
|
|
3
index.js
3
index.js
|
@ -1,6 +1,7 @@
|
|||
module.exports = {
|
||||
db: require('./lib/db'),
|
||||
errors: require('./lib/errors.js'),
|
||||
errors: require('./lib/errors.js').errorsGen,
|
||||
errorsClean: require('./lib/errors.js').errorsClean,
|
||||
shuffle: require('./lib/shuffle'),
|
||||
stringHash: require('./lib/stringHash'),
|
||||
};
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
'use strict'; // eslint-disable-line strict
|
||||
|
||||
const errors = {};
|
||||
|
||||
class ArsenalError extends Error {
|
||||
constructor(type, code, desc) {
|
||||
super(type);
|
||||
|
@ -7,6 +9,40 @@ class ArsenalError extends Error {
|
|||
this.description = desc;
|
||||
this[type] = true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Map errors from MD to S3
|
||||
* @returns {Object.<ArsenalError>} - the instance of the S3 corresponding
|
||||
* error
|
||||
*/
|
||||
errorsMap() {
|
||||
const map = {
|
||||
NoSuchBucket: 'NoSuchBucket',
|
||||
BucketAlreadyExists: 'BucketAlreadyExists',
|
||||
NoSuchKey: 'NoSuchKey',
|
||||
DBNotFound: 'NoSuchBucket',
|
||||
DBAlreadyExists: 'BucketAlreadyExists',
|
||||
ObjNotFound: 'NoSuchKey',
|
||||
NotImplemented: 'NotImplemented',
|
||||
};
|
||||
return errors[map[this.message]] ? errors[map[this.message]] :
|
||||
errors.InternalError;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Clean errors from vaultClient to S3
|
||||
* @param {Object} obj - the vaultclient error
|
||||
* @returns {Object.<ArsenalError>} - the instance of the S3 corresponding
|
||||
* error
|
||||
*/
|
||||
function errorsClean(obj) {
|
||||
let err;
|
||||
Object.keys(obj.message).forEach(prop => {
|
||||
if (obj.message[prop] === true)
|
||||
err = errors[prop];
|
||||
});
|
||||
return (err);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -16,7 +52,6 @@ class ArsenalError extends Error {
|
|||
* instances
|
||||
*/
|
||||
function errorsGen() {
|
||||
const errors = {};
|
||||
const errorsObj = require('../errors/arsenalErrors.json');
|
||||
|
||||
Object.keys(errorsObj)
|
||||
|
@ -28,4 +63,7 @@ function errorsGen() {
|
|||
return errors;
|
||||
}
|
||||
|
||||
module.exports = errorsGen();
|
||||
module.exports = {
|
||||
errorsGen: errorsGen(),
|
||||
errorsClean,
|
||||
};
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
"main": "index.js",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "git+https://github.com/scality/IronMan-Arsenal.git"
|
||||
"url": "git+https://github.com/scality/Arsenal.git"
|
||||
},
|
||||
"contributors": [
|
||||
{ "name": "David Pineau", "email": "" },
|
||||
|
@ -14,15 +14,15 @@
|
|||
],
|
||||
"license": "ISC",
|
||||
"bugs": {
|
||||
"url": "https://github.com/scality/IronMan-Arsenal/issues"
|
||||
"url": "https://github.com/scality/Arsenal/issues"
|
||||
},
|
||||
"homepage": "https://github.com/scality/IronMan-Arsenal#readme",
|
||||
"homepage": "https://github.com/scality/Arsenal#readme",
|
||||
"dependencies": {
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^2.4.0",
|
||||
"eslint-config-airbnb": "^6.0.0",
|
||||
"eslint-config-ironman": "scality/IronMan-Guidelines#rel/1.0",
|
||||
"eslint-config-ironman": "scality/Guidelines#rel/1.1",
|
||||
"level": "^1.3.0",
|
||||
"mocha": "^2.3.3",
|
||||
"temp": "^0.8.3"
|
||||
|
|
|
@ -3,6 +3,17 @@ const assert = require('assert');
|
|||
const errorsJSON = require('../../errors/arsenalErrors.json');
|
||||
const errors = require('../../index').errors;
|
||||
|
||||
const map = {
|
||||
NoSuchBucket: 'NoSuchBucket',
|
||||
BucketAlreadyExists: 'BucketAlreadyExists',
|
||||
NoSuchKey: 'NoSuchKey',
|
||||
DBNotFound: 'NoSuchBucket',
|
||||
DBAlreadyExists: 'BucketAlreadyExists',
|
||||
ObjNotFound: 'NoSuchKey',
|
||||
NotImplemented: 'NotImplemented',
|
||||
};
|
||||
|
||||
|
||||
describe('Runtime errors instance generation', () => {
|
||||
Object.keys(errors).forEach(index => {
|
||||
it(`should return and instance of ${index} Error`, done => {
|
||||
|
@ -19,3 +30,13 @@ describe('Runtime errors instance generation', () => {
|
|||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('Error map(wrapper) MD -> S3', () => {
|
||||
Object.keys(map).forEach(index => {
|
||||
it(`should return an instance of ${map[index]} Error`, done => {
|
||||
assert.deepStrictEqual(errors[index].errorsMap(),
|
||||
errors[map[index]]);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Reference in New Issue