Skip to content

Commit

Permalink
Merge pull request #26 from scality/dev/ft/errors
Browse files Browse the repository at this point in the history
(errors) Implement arsenal errors in  RESTClient
  • Loading branch information
AntoninCoulibaly committed Mar 8, 2016
2 parents 3fe408f + 1d7fe86 commit e1e2a45
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
18 changes: 16 additions & 2 deletions lib/RESTClient.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const errors = require('arsenal').errors;
const assert = require('assert');
const http = require('http');
const querystring = require('querystring');
Expand All @@ -9,6 +10,19 @@ const Logger = require('werelogs');
const shuffle = require('arsenal').shuffle;
const conf = require('../config.json');

function errorMap(mdError) {
const map = {
NoSuchBucket: 'NoSuchBucket',
BucketAlreadyExists: 'BucketAlreadyExists',
NoSuchKey: 'NoSuchKey',
DBNotFound: 'NoSuchBucket',
DBAlreadyExists: 'BucketAlreadyExists',
ObjNotFound: 'NoSuchKey',
NotImplemented: 'NotImplemented',
};
return map[mdError] ? map[mdError] : 'InternalError';
}

class RESTClient {
/**
* Constructor for a REST client to bucketd
Expand Down Expand Up @@ -196,9 +210,9 @@ class RESTClient {
`code=${code} ret=${ret}`);
return callback(null, ret);
}
const error = new Error(res.statusMessage);

const error = errors[errorMap(res.statusMessage)];
error.isExpected = true;
error.code = code;
log.debug(`direct request to endpoint returned an expected ` +
`error code=${res.statusCode} ret=${res.statusMessage}`);
return callback(error, ret);
Expand Down
13 changes: 5 additions & 8 deletions tests/unit/simple_tests.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use strict';

const errors = require('arsenal').errors;
const assert = require('assert');
const http = require('http');
const RESTClient = require('../../index.js').RESTClient;
Expand Down Expand Up @@ -70,9 +71,8 @@ describe('Unit tests with mockup server', function tests() {
it('should try to create an already existing bucket and fail', done => {
client.createBucket(existBucket.name, reqUids, '{}', (err) => {
if (err) {
const error = new Error('BucketAlreadyExists');
const error = errors.BucketAlreadyExists;
error.isExpected = true;
error.code = 409;
assert.deepStrictEqual(err, error);
return done();
}
Expand All @@ -98,9 +98,8 @@ describe('Unit tests with mockup server', function tests() {

it('should get Raft informations on an unexisting bucket', done => {
client.getRaftInformation(nonExistBucket.name, reqUids, (err, data) => {
const error = new Error('NoSuchBucket');
const error = errors.NoSuchBucket;
error.isExpected = true;
error.code = 404;
assert.deepStrictEqual(err, error);
return done();
done(err);
Expand All @@ -110,9 +109,8 @@ describe('Unit tests with mockup server', function tests() {
it('should fetch non-existing bucket, sending back an error', done => {
client.getBucketAttributes(nonExistBucket.name, reqUids, (err) => {
if (err) {
const error = new Error('NoSuchBucket');
const error = errors.NoSuchBucket;
error.isExpected = true;
error.code = 404;
assert.deepStrictEqual(err, error);
return done();
}
Expand All @@ -127,9 +125,8 @@ describe('Unit tests with mockup server', function tests() {
it('should fetch non-existing bucket, sending back an error', done => {
client.deleteBucket(nonExistBucket.name, reqUids, err => {
if (err) {
const error = new Error('NoSuchBucket');
const error = errors.NoSuchBucket;
error.isExpected = true;
error.code = 404;
assert.deepStrictEqual(err, error);
return done();
}
Expand Down

0 comments on commit e1e2a45

Please sign in to comment.