Compare commits

...

2 Commits

Author SHA1 Message Date
David Pineau 37efe0c617 Fixup - Remove IRM from package.json 2016-06-20 20:32:27 +02:00
Antonin Coulibaly fc61e60bd5 Implement first draft of JSON translation
* Update JSON to add S3 translations
* update errors.js for having a simple translate method
* update associated tests
* Doc :
  ```js
    import { errors } from 'arsenal'

    console.log(errors.DBNotFound.toS3());
    // { [Error: NoSuchBucket]
    //     code: 404,
    //     description: 'The specified bucket does not exist.',
    //     NoSuchBucket: true }
  ```
2016-05-04 14:27:57 +02:00
5 changed files with 106 additions and 27 deletions

View File

@ -46,3 +46,17 @@ 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,50 +618,86 @@
"_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,11 +1,23 @@
'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, translation) {
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;
} }
} }
@ -16,14 +28,13 @@ 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].description, errorsObj[index].translation);
}); });
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/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

@ -19,3 +19,21 @@ 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();
});
});
});