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

@ -293,13 +293,13 @@
"description": "The request signature we calculated does not match the signature you provided." "description": "The request signature we calculated does not match the signature you provided."
}, },
"_comment" : { "_comment" : {
"note" : "This is an AWS S3 specific error. We are opting to use the more general 'ServiceUnavailable' error used throughout AWS (IAM/EC2) to have uniformity of error messages even though we are potentially compromising S3 compatibility.", "note" : "This is an AWS S3 specific error. We are opting to use the more general 'ServiceUnavailable' error used throughout AWS (IAM/EC2) to have uniformity of error messages even though we are potentially compromising S3 compatibility.",
"ServiceUnavailable": { "ServiceUnavailable": {
"code": 503, "code": 503,
"description": "Reduce your request rate." "description": "Reduce your request rate."
} }
}, },
"ServiceUnavailable": { "ServiceUnavailable": {
"code": 503, "code": 503,
"description": "The request has failed due to a temporary failure of the server." "description": "The request has failed due to a temporary failure of the server."
}, },
@ -550,7 +550,7 @@
"SecretKeyDoesNotExist": { "SecretKeyDoesNotExist": {
"description": "secret key does not exist", "description": "secret key does not exist",
"code": 5030 "code": 5030
}, },
"InvalidRegion": { "InvalidRegion": {
"description": "Region was not provided or is not recognized by the system", "description": "Region was not provided or is not recognized by the system",
"code": 5031 "code": 5031
@ -583,15 +583,15 @@
"BadUrl": { "BadUrl": {
"description": "url not ok", "description": "url not ok",
"code": 5038 "code": 5038
}, },
"BadClientIdList": { "BadClientIdList": {
"description": "client id list not ok'", "description": "client id list not ok'",
"code": 5039 "code": 5039
}, },
"BadThumbprintList": { "BadThumbprintList": {
"description": "thumbprint list not ok'", "description": "thumbprint list not ok'",
"code": 5040 "code": 5040
}, },
"BadObject": { "BadObject": {
"description": "Object not ok'", "description": "Object not ok'",
"code": 5041 "code": 5041
@ -600,12 +600,12 @@
"BadRole": { "BadRole": {
"description": "role not ok", "description": "role not ok",
"code": 5042 "code": 5042
}, },
"_comment": "#### SamlpErrors ####", "_comment": "#### SamlpErrors ####",
"BadSamlp": { "BadSamlp": {
"description": "samlp not ok", "description": "samlp not ok",
"code": 5043 "code": 5043
}, },
"BadMetadataDocument": { "BadMetadataDocument": {
"description": "metadata document not ok", "description": "metadata document not ok",
"code": 5044 "code": 5044
@ -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();
});
});
});