Skip to content

Commit 245eb07

Browse files
authored
Merge pull request #905 from swagger-api/issue-904
use errors with promises
2 parents a6d0ec2 + 54617c2 commit 245eb07

File tree

7 files changed

+40
-5
lines changed

7 files changed

+40
-5
lines changed

browser/swagger-client.js

Lines changed: 7 additions & 2 deletions
Large diffs are not rendered by default.

browser/swagger-client.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/http.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,12 @@ SwaggerHttp.prototype.execute = function (obj, opts) {
9090
};
9191

9292
obj.on.response = function(data) {
93-
responseInterceptor(data);
93+
if(data && data.status >= 400) {
94+
errorInterceptor(data);
95+
}
96+
else {
97+
responseInterceptor(data);
98+
}
9499
};
95100

96101
if (_.isObject(obj) && _.isObject(obj.body)) {

test/client.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,6 +1405,23 @@ describe('SwaggerClient', function () {
14051405
});
14061406
});
14071407

1408+
it('should catch an error', function(done) {
1409+
new SwaggerClient({
1410+
url: 'http://localhost:8000/v2/issue-716.yaml',
1411+
usePromise: true
1412+
}).then(function(client) {
1413+
client.Data.getPets()
1414+
.then(function(data) {
1415+
done('shoulda failed');
1416+
})
1417+
.catch(function(err) {
1418+
done();
1419+
})
1420+
}).catch(function(exception) {
1421+
done(exception);
1422+
});
1423+
});
1424+
14081425
it('should read a blob', function(done) {
14091426
var spec = {
14101427
paths: {

test/mock.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ exports.petstore = function (arg1, arg2, arg3, arg4) {
102102

103103
return;
104104
} else {
105-
res.writeHead(200, {'Content-Type': 'text/plain'});
105+
res.writeHead(404, {'Content-Type': 'text/plain'});
106106
res.write('404 Not Found\n');
107107
res.end();
108108
}

test/request.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,12 @@ describe('swagger request functions', function () {
365365
expect(resp.headers['content-type']).toNotBe(undefined);
366366
expect(resp.headers['content-type']).toBe('text/plain');
367367

368+
done('should have failed');
369+
}, function(err) {
370+
expect(err.headers).toNotBe(undefined);
371+
expect(err.headers['content-type']).toNotBe(undefined);
372+
expect(err.headers['content-type']).toBe('text/plain');
373+
368374
done();
369375
});
370376
});

test/spec/v2/issue-716.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ basePath: /
66
paths:
77
/linked:
88
get:
9+
tags:
10+
- Data
911
description: Returns a list of pets
1012
operationId: getPets
1113
responses:

0 commit comments

Comments
 (0)