Compare commits

..

No commits in common. "37efe0c617ceaeb10f92d531f3c6a4f1af2edec8" and "bf7af082feb8adcaea32098e28bc5e510cbb44a7" have entirely different histories.

5 changed files with 27 additions and 106 deletions

View File

@ -46,17 +46,3 @@ console.log(errors.AccessDenied);
// AccessDenied: true } // AccessDenied: true }
``` ```
#### Map an error
``` js
import { errors } from 'arsenal';
console.log(errors.DBNotFound.toS3());
// { [Error: NoSuchBucket]
// code: 404,
// description: 'The specified bucket does not exist.',
// NoSuchBucket: true }
```

View File

@ -618,86 +618,50 @@
"_comment": "#### formatErrors ####", "_comment": "#### formatErrors ####",
"DBNotFound": { "DBNotFound": {
"description": "This DB does not exist", "description": "This DB does not exist",
"code": 404, "code": 404
"translation": {
"S3": "NoSuchBucket"
}
}, },
"DBAlreadyExists": { "DBAlreadyExists": {
"description": "This DB already exist", "description": "This DB already exist",
"code": 409, "code": 409
"translation": {
"S3": "BucketAlreadyExists"
}
}, },
"ObjNotFound": { "ObjNotFound": {
"description": "This object does not exist", "description": "This object does not exist",
"code": 404, "code": 404
"translation": {
"S3": "NoSuchKey"
}
}, },
"PermissionDenied": { "PermissionDenied": {
"description": "Permission denied", "description": "Permission denied",
"code": 403, "code": 403
"translation": {
"S3": "PermissionDenied"
}
}, },
"BadRequest": { "BadRequest": {
"description": "BadRequest", "description": "BadRequest",
"code": 400, "code": 400
"translation": {
"S3": "BadRequest"
}
}, },
"RaftSessionNotLeader": { "RaftSessionNotLeader": {
"description": "NotLeader", "description": "NotLeader",
"code": 500, "code": 500
"translation": {
"S3": "RaftSessionNotLeader"
}
}, },
"RaftSessionLeaderNotConnected": { "RaftSessionLeaderNotConnected": {
"description": "RaftSessionLeaderNotConnected", "description": "RaftSessionLeaderNotConnected",
"code": 400, "code": 400
"translation": {
"S3": "RaftSessionLeaderNotConnected"
}
}, },
"NoLeaderForDB": { "NoLeaderForDB": {
"description": "NoLeaderForDB", "description": "NoLeaderForDB",
"code": 400, "code": 400
"translation": {
"S3": "NoLeaderForDB"
}
}, },
"RouteNotFound": { "RouteNotFound": {
"description": "RouteNotFound", "description": "RouteNotFound",
"code": 404, "code": 404
"translation": {
"S3": "RouteNotFound"
}
}, },
"NoMapsInConfig": { "NoMapsInConfig": {
"description": "NoMapsInConfig", "description": "NoMapsInConfig",
"code": 404, "code": 404
"translation": {
"S3": "NoMapsInConfig"
}
}, },
"DBAPINotReady": { "DBAPINotReady": {
"message": "DBAPINotReady", "message": "DBAPINotReady",
"code": 500, "code": 500
"translation": {
"S3": "DBAPINotReady"
}
}, },
"NotEnoughMapsInConfig:": { "NotEnoughMapsInConfig:": {
"description": "NotEnoughMapsInConfig", "description": "NotEnoughMapsInConfig",
"code": 400, "code": 400
"translation": {
"S3": "NotEnoughMapsInConfig:"
}
} }
} }

View File

@ -1,23 +1,11 @@
'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, translation) { constructor(type, code, desc) {
super(type); super(type);
this.code = code; this.code = code;
this.description = desc; this.description = desc;
this[type] = true; this[type] = true;
this.translation = translation;
}
/**
* Translate errors to S3
* @returns {Object.<ArsenalError>} - the instance of the S3 corresponding
* error
*/
toS3() {
return this.translation ? errors[this.translation.S3] : this;
} }
} }
@ -28,13 +16,14 @@ 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)
.filter(index => index !== '_comment') .filter(index => index !== '_comment')
.forEach(index => { .forEach(index => {
errors[index] = new ArsenalError(index, errorsObj[index].code, errors[index] = new ArsenalError(index, errorsObj[index].code,
errorsObj[index].description, errorsObj[index].translation); errorsObj[index].description);
}); });
return errors; return errors;
} }

View File

@ -5,7 +5,7 @@
"main": "index.js", "main": "index.js",
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git+https://github.com/scality/Arsenal.git" "url": "git+https://github.com/scality/IronMan-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/Arsenal/issues" "url": "https://github.com/scality/IronMan-Arsenal/issues"
}, },
"homepage": "https://github.com/scality/Arsenal#readme", "homepage": "https://github.com/scality/IronMan-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/Guidelines#rel/1.1", "eslint-config-ironman": "scality/IronMan-Guidelines#rel/1.0",
"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

@ -19,21 +19,3 @@ describe('Runtime errors instance generation', () => {
}); });
}); });
}); });
describe('Error translation', () => {
Object.keys(errors).forEach(index => {
const err = errors[index].translation ?
errors[errors[index].translation.S3].message :
errors[index].message;
it(`should return the S3 translation of ${errors[index]} (${err})`,
done => {
if (errors[index].translation) {
assert.deepStrictEqual(errors[errors[index].translation.S3],
errors[index].toS3());
} else {
assert.deepStrictEqual(errors[index], errors[index].toS3());
}
done();
});
});
});