Skip to content

Commit

Permalink
Send 400 on every error (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Giordano authored and Giovanni Toraldo committed Mar 10, 2017
1 parent 284eb00 commit 1413db1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
12 changes: 6 additions & 6 deletions src/server.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const http = require('http');
const express = require('express');
const cors = require('cors');
const Vibrant = require('node-vibrant');
const express = require('express');
const http = require('http');
const request = require('request');
const Vibrant = require('node-vibrant');

const app = express();
const server = http.createServer(app);
Expand All @@ -12,7 +12,7 @@ app.use(cors());
app.get('/', (req, res, next) => {
const imgUrl = req.query.imgUrl;

if (imgUrl == undefined || imgUrl == '') {
if (!imgUrl) {
let err = new Error('missing `imgUrl` parameter');
err.status = 400;
throw err;
Expand All @@ -23,13 +23,13 @@ app.get('/', (req, res, next) => {
encoding: null
}, (error, response, body) => {
if (error != null) {
res.status(500).send(error);
res.status(400).send(error);
return next(error);
}

Vibrant.from(body).getPalette((err, palette) => {
if (err != null) {
res.status(500).send(err.toString());
res.status(400).send(err.toString());
return next(err);
}

Expand Down
6 changes: 3 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('loading express', () => {
res.body.LightMuted.rgb.should.containDeep([153, 170, 171]);
done();
});
});
}).timeout(5000);

it('responds to preflight cors requests', (done) => {
request(server)
Expand Down Expand Up @@ -64,9 +64,9 @@ describe('loading express', () => {
.expect(404, done);
});

it('500 when not 200 or 300', (done) => {
it('400 when not 200 or 300', (done) => {
request(server)
.get('/?imgUrl=https://httpbin.org/status/418')
.expect(500, done);
.expect(400, done);
}).timeout(5000);
});

0 comments on commit 1413db1

Please sign in to comment.