Skip to content

Commit 6a8cb13

Browse files
committed
Custom error attribute (code), closes #4
1 parent 0d5b181 commit 6a8cb13

4 files changed

Lines changed: 36 additions & 7 deletions

File tree

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Akamai NetStorage HTTP API for the Node.js
55
## Installation
66
```
77
"dependencies": {
8-
"akamai-http-api": "0.2.2"
8+
"akamai-http-api": "0.3.*"
99
}
1010
```
1111
```npm update```
@@ -85,6 +85,20 @@ akamai.symlink('/12345/MyFile.jpg', '/12345/MyFileSymlink.jpg', function (err, d
8585
akamai.fileExists('/12345/MyFile.jpg', function (err, boolFlag) {});
8686
```
8787

88+
### Exceptions
89+
For the communication netstorage HTTP API uses [HTTP codes](http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html).
90+
Hence, a number of methods may trigger an exception. For example `mkdir` in case the target already exists.
91+
Or `symlink` in case the target doesn't exist.
92+
93+
To handle these exceptions the `err` object has an abnormal `code` attribute.
94+
95+
```javascript
96+
akamai.mkdir('...', function (err, data) {
97+
if (err.code === 409) { } // it already exists
98+
if (err.message.indexOf(409) !== -1) // the same
99+
});
100+
```
101+
88102
### How to extend it?
89103
```javascript
90104
var akamai = require('akamai-http-api'),
@@ -117,6 +131,7 @@ module.exports = myAkamai;
117131

118132
## Docker
119133
```sh
134+
# modify test/akamai.js#15 first
120135
[sudo] docker build -t node-akamai-http-api .
121136
[sudo] docker run -ti --rm node-akamai-http-api
122137
```

lib/akamai.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -147,18 +147,19 @@ akamai.getRequestObject = function (path, params, cb) {
147147

148148
if (typeof(cb) === 'function') {
149149
callback = function (err, resp, body) {
150-
// we have an error
151150
if (err) {
152151
return cb(err);
153152
}
154153

155154
// wrong response code
156155
if (resp.statusCode >= 300) {
157-
var errMsg = 'The server sent us the ' + resp.statusCode + ' code';
156+
var exception = new Error();
157+
exception.code = resp.statusCode;
158+
exception.message = 'The server sent us the ' + resp.statusCode + ' code';
158159
if (self.getConfig().verbose && body) {
159-
errMsg += '. Body: ' + body;
160+
exception.message += '. Body: ' + body;
160161
}
161-
return cb(new Error(errMsg));
162+
return cb(exception);
162163
}
163164

164165
if (!body.match(/^<\?xml\s+/)) {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"name": "akamai-http-api",
3-
"version": "0.2.2",
3+
"version": "0.3.0",
44
"main": "./lib/akamai.js",
55
"author": "Kanstantsin Kamkou <kkamkou@gmail.com>",
66
"description": "Akamai NetStorage HTTP API for the Node.js",
7-
"keywords": ["akamai", "api", "http"],
7+
"keywords": ["akamai", "api", "http", "netstorage"],
88
"repository" : {
99
"type" : "git",
1010
"url" : "https://github.com/kkamkou/node-akamai-http-api.git"

test/akamai.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,10 @@ module.exports = {
5252
var clone = Object.create(akamai);
5353
clone.setConfig({key: 'invalidKey', verbose: true})
5454
.du(path.dirname(pathRemoteFile), function (err) {
55+
err.should.be.an.instanceof(Error);
5556
err.message.should.include("You don't have permission to access");
5657
err.message.should.include(403);
58+
err.code.should.eql(403);
5759
done();
5860
});
5961
}
@@ -74,6 +76,7 @@ module.exports = {
7476
'Folder does not exist': function (done) {
7577
akamai.du('AnEpicFolder', function (err) {
7678
err.message.should.include(404);
79+
err.code.should.eql(404);
7780
done();
7881
});
7982
}
@@ -94,6 +97,7 @@ module.exports = {
9497
'Folder does not exist': function (done) {
9598
akamai.dir('AnEpicFolder', function (err) {
9699
err.message.should.include(404);
100+
err.code.should.eql(404);
97101
done();
98102
});
99103
}
@@ -118,6 +122,7 @@ module.exports = {
118122

119123
akamai.upload(stream, '/1234', function (err) {
120124
err.message.should.include(404);
125+
err.code.should.eql(404);
121126
done();
122127
});
123128
}
@@ -141,6 +146,7 @@ module.exports = {
141146

142147
akamai.download('AnEpicFile.txt', stream, function (err) {
143148
err.message.should.include(404);
149+
err.code.should.eql(404);
144150
done();
145151
});
146152
}
@@ -158,6 +164,7 @@ module.exports = {
158164
'File does not exist': function (done) {
159165
akamai.mtime('AnEpicFile.txt', new Date(), function (err) {
160166
err.message.should.include(404);
167+
err.code.should.eql(404);
161168
done();
162169
});
163170
}
@@ -178,6 +185,7 @@ module.exports = {
178185
'File does not exist': function (done) {
179186
akamai.stat('AnEpicFile.txt', function (err) {
180187
err.message.should.include(404);
188+
err.code.should.eql(404);
181189
done();
182190
});
183191
}
@@ -214,6 +222,7 @@ module.exports = {
214222
'File does not exist': function (done) {
215223
akamai.symlink(pathRemoteFile + '.fake', pathRemoteFile + '.symlink', function (err) {
216224
err.message.should.include(409);
225+
err.code.should.eql(409);
217226
done();
218227
});
219228
}
@@ -231,6 +240,7 @@ module.exports = {
231240
'Folder exists': function (done) {
232241
akamai.mkdir(pathRemoteDir, function (err) {
233242
err.message.should.include(409);
243+
err.code.should.eql(409);
234244
done();
235245
});
236246
}
@@ -248,6 +258,7 @@ module.exports = {
248258
'Folder does not exist': function (done) {
249259
akamai.rmdir(pathRemoteDir, function (err) {
250260
err.message.should.include(404);
261+
err.code.should.eql(404);
251262
done();
252263
});
253264
}
@@ -265,6 +276,7 @@ module.exports = {
265276
'File does not exist': function (done) {
266277
akamai.rename(pathRemoteFile, pathRemoteFile + '.renamed', function (err) {
267278
err.message.should.include(404);
279+
err.code.should.eql(404);
268280
done();
269281
});
270282
}
@@ -283,6 +295,7 @@ module.exports = {
283295
'File does not exist': function (done) {
284296
akamai.delete(pathRemoteFile + '.renamed', function (err) {
285297
err.message.should.include(404);
298+
err.code.should.eql(404);
286299
done();
287300
});
288301
}

0 commit comments

Comments
 (0)