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 }
```
#### 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."
},
"_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": {
"code": 503,
"description": "Reduce your request rate."
}
},
"ServiceUnavailable": {
"ServiceUnavailable": {
"code": 503,
"description": "The request has failed due to a temporary failure of the server."
},
@ -550,7 +550,7 @@
"SecretKeyDoesNotExist": {
"description": "secret key does not exist",
"code": 5030
},
},
"InvalidRegion": {
"description": "Region was not provided or is not recognized by the system",
"code": 5031
@ -583,15 +583,15 @@
"BadUrl": {
"description": "url not ok",
"code": 5038
},
},
"BadClientIdList": {
"description": "client id list not ok'",
"code": 5039
},
},
"BadThumbprintList": {
"description": "thumbprint list not ok'",
"code": 5040
},
},
"BadObject": {
"description": "Object not ok'",
"code": 5041
@ -600,12 +600,12 @@
"BadRole": {
"description": "role not ok",
"code": 5042
},
},
"_comment": "#### SamlpErrors ####",
"BadSamlp": {
"description": "samlp not ok",
"code": 5043
},
},
"BadMetadataDocument": {
"description": "metadata document not ok",
"code": 5044
@ -618,50 +618,86 @@
"_comment": "#### formatErrors ####",
"DBNotFound": {
"description": "This DB does not exist",
"code": 404
"code": 404,
"translation": {
"S3": "NoSuchBucket"
}
},
"DBAlreadyExists": {
"description": "This DB already exist",
"code": 409
"code": 409,
"translation": {
"S3": "BucketAlreadyExists"
}
},
"ObjNotFound": {
"description": "This object does not exist",
"code": 404
"code": 404,
"translation": {
"S3": "NoSuchKey"
}
},
"PermissionDenied": {
"description": "Permission denied",
"code": 403
"code": 403,
"translation": {
"S3": "PermissionDenied"
}
},
"BadRequest": {
"description": "BadRequest",
"code": 400
"code": 400,
"translation": {
"S3": "BadRequest"
}
},
"RaftSessionNotLeader": {
"description": "NotLeader",
"code": 500
"code": 500,
"translation": {
"S3": "RaftSessionNotLeader"
}
},
"RaftSessionLeaderNotConnected": {
"description": "RaftSessionLeaderNotConnected",
"code": 400
"code": 400,
"translation": {
"S3": "RaftSessionLeaderNotConnected"
}
},
"NoLeaderForDB": {
"description": "NoLeaderForDB",
"code": 400
"code": 400,
"translation": {
"S3": "NoLeaderForDB"
}
},
"RouteNotFound": {
"description": "RouteNotFound",
"code": 404
"code": 404,
"translation": {
"S3": "RouteNotFound"
}
},
"NoMapsInConfig": {
"description": "NoMapsInConfig",
"code": 404
"code": 404,
"translation": {
"S3": "NoMapsInConfig"
}
},
"DBAPINotReady": {
"message": "DBAPINotReady",
"code": 500
"code": 500,
"translation": {
"S3": "DBAPINotReady"
}
},
"NotEnoughMapsInConfig:": {
"description": "NotEnoughMapsInConfig",
"code": 400
"code": 400,
"translation": {
"S3": "NotEnoughMapsInConfig:"
}
}
}

View File

@ -1,11 +1,23 @@
'use strict'; // eslint-disable-line strict
const errors = {};
class ArsenalError extends Error {
constructor(type, code, desc) {
constructor(type, code, desc, translation) {
super(type);
this.code = code;
this.description = desc;
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
*/
function errorsGen() {
const errors = {};
const errorsObj = require('../errors/arsenalErrors.json');
Object.keys(errorsObj)
.filter(index => index !== '_comment')
.forEach(index => {
errors[index] = new ArsenalError(index, errorsObj[index].code,
errorsObj[index].description);
errorsObj[index].description, errorsObj[index].translation);
});
return errors;
}

View File

@ -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"

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();
});
});
});