Compare commits

...

4 Commits

Author SHA1 Message Date
David Pineau 44c18c1423 Fixup - Remove IRM from package.json 2016-06-20 20:33:08 +02:00
Antonin Coulibaly 9adc02a6ba (errors) Implement vault error clean 2016-04-25 14:37:47 +02:00
Antonin Coulibaly 12c5d362c6 (mapping) Update documentation 2016-04-25 11:24:58 +02:00
Antonin Coulibaly ea4eb6a4fb Implement error map
* Implement associated unit tests
2016-04-25 11:24:58 +02:00
5 changed files with 83 additions and 7 deletions

View File

@ -35,6 +35,8 @@ console.log(array);
### Usage ### Usage
#### Get an error
``` js ``` js
import { errors } from 'arsenal'; import { errors } from 'arsenal';
@ -46,3 +48,17 @@ console.log(errors.AccessDenied);
// AccessDenied: true } // 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 }
```

View File

@ -1,6 +1,7 @@
module.exports = { module.exports = {
db: require('./lib/db'), 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'), shuffle: require('./lib/shuffle'),
stringHash: require('./lib/stringHash'), stringHash: require('./lib/stringHash'),
}; };

View File

@ -1,5 +1,7 @@
'use strict'; // eslint-disable-line strict 'use strict'; // eslint-disable-line strict
const errors = {};
class ArsenalError extends Error { class ArsenalError extends Error {
constructor(type, code, desc) { constructor(type, code, desc) {
super(type); super(type);
@ -7,6 +9,40 @@ class ArsenalError extends Error {
this.description = desc; this.description = desc;
this[type] = true; 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 * instances
*/ */
function errorsGen() { function errorsGen() {
const errors = {};
const errorsObj = require('../errors/arsenalErrors.json'); const errorsObj = require('../errors/arsenalErrors.json');
Object.keys(errorsObj) Object.keys(errorsObj)
@ -28,4 +63,7 @@ function errorsGen() {
return errors; return errors;
} }
module.exports = errorsGen(); module.exports = {
errorsGen: errorsGen(),
errorsClean,
};

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/scality/IronMan-Arsenal.git" "url": "git+https://github.com/scality/Arsenal.git"
}, },
"contributors": [ "contributors": [
{ "name": "David Pineau", "email": "" }, { "name": "David Pineau", "email": "" },
@ -14,15 +14,15 @@
], ],
"license": "ISC", "license": "ISC",
"bugs": { "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": { "dependencies": {
}, },
"devDependencies": { "devDependencies": {
"eslint": "^2.4.0", "eslint": "^2.4.0",
"eslint-config-airbnb": "^6.0.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", "level": "^1.3.0",
"mocha": "^2.3.3", "mocha": "^2.3.3",
"temp": "^0.8.3" "temp": "^0.8.3"

View File

@ -3,6 +3,17 @@ const assert = require('assert');
const errorsJSON = require('../../errors/arsenalErrors.json'); const errorsJSON = require('../../errors/arsenalErrors.json');
const errors = require('../../index').errors; 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', () => { describe('Runtime errors instance generation', () => {
Object.keys(errors).forEach(index => { Object.keys(errors).forEach(index => {
it(`should return and instance of ${index} Error`, done => { 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();
});
});
});