Skip to content

Commit d3637f2

Browse files
committed
Manage 404 errors
1 parent 89c5ce3 commit d3637f2

File tree

5 files changed

+106
-7
lines changed

5 files changed

+106
-7
lines changed

api/doc/api_data.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@ define({ "api": [
305305
}
306306
]
307307
},
308+
"error": {
309+
"fields": {
310+
"Error 4xx": [
311+
{
312+
"group": "Error 4xx",
313+
"type": "String",
314+
"optional": false,
315+
"field": "404",
316+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
317+
}
318+
]
319+
}
320+
},
308321
"version": "0.0.0",
309322
"filename": "src/server.js",
310323
"groupTitle": "Wines"
@@ -328,6 +341,19 @@ define({ "api": [
328341
]
329342
}
330343
},
344+
"error": {
345+
"fields": {
346+
"Error 4xx": [
347+
{
348+
"group": "Error 4xx",
349+
"type": "String",
350+
"optional": false,
351+
"field": "404",
352+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
353+
}
354+
]
355+
}
356+
},
331357
"version": "0.0.0",
332358
"filename": "src/server.js",
333359
"groupTitle": "Wines"
@@ -376,6 +402,19 @@ define({ "api": [
376402
}
377403
]
378404
},
405+
"error": {
406+
"fields": {
407+
"Error 4xx": [
408+
{
409+
"group": "Error 4xx",
410+
"type": "String",
411+
"optional": false,
412+
"field": "404",
413+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
414+
}
415+
]
416+
}
417+
},
379418
"version": "0.0.0",
380419
"filename": "src/server.js",
381420
"groupTitle": "Wines"

api/doc/api_data.json

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,19 @@
305305
}
306306
]
307307
},
308+
"error": {
309+
"fields": {
310+
"Error 4xx": [
311+
{
312+
"group": "Error 4xx",
313+
"type": "String",
314+
"optional": false,
315+
"field": "404",
316+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
317+
}
318+
]
319+
}
320+
},
308321
"version": "0.0.0",
309322
"filename": "src/server.js",
310323
"groupTitle": "Wines"
@@ -328,6 +341,19 @@
328341
]
329342
}
330343
},
344+
"error": {
345+
"fields": {
346+
"Error 4xx": [
347+
{
348+
"group": "Error 4xx",
349+
"type": "String",
350+
"optional": false,
351+
"field": "404",
352+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
353+
}
354+
]
355+
}
356+
},
331357
"version": "0.0.0",
332358
"filename": "src/server.js",
333359
"groupTitle": "Wines"
@@ -376,6 +402,19 @@
376402
}
377403
]
378404
},
405+
"error": {
406+
"fields": {
407+
"Error 4xx": [
408+
{
409+
"group": "Error 4xx",
410+
"type": "String",
411+
"optional": false,
412+
"field": "404",
413+
"description": "<p>Not found - No wine corresponding to given 'id'</p>"
414+
}
415+
]
416+
}
417+
},
379418
"version": "0.0.0",
380419
"filename": "src/server.js",
381420
"groupTitle": "Wines"

api/doc/api_project.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ define({
88
"apidoc": "0.2.0",
99
"generator": {
1010
"name": "apidoc",
11-
"time": "2016-03-13T20:50:54.459Z",
11+
"time": "2016-03-13T21:01:05.404Z",
1212
"url": "http://apidocjs.com",
1313
"version": "0.15.1"
1414
}

api/doc/api_project.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"apidoc": "0.2.0",
99
"generator": {
1010
"name": "apidoc",
11-
"time": "2016-03-13T20:50:54.459Z",
11+
"time": "2016-03-13T21:01:05.404Z",
1212
"url": "http://apidocjs.com",
1313
"version": "0.15.1"
1414
}

api/src/server.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ ChampagneWines.forEach(byId);
2626
LoireWines.forEach(byId);
2727

2828
// Likes and Comments
29-
var Likes = [];
29+
var Likes = ["chevrol-bel-air"];
3030
var Comments = {
3131
"chevrol-bel-air": [{
3232
"date": new Date(),
@@ -140,10 +140,17 @@ app.get('/api/wines/:id', function (req, res) {
140140
* @apiGroup Wines
141141
*
142142
* @apiParam {String} id the id of the wine
143+
*
144+
* @apiError {String} 404 Not found - No wine corresponding to given 'id'
143145
*/
144146
app.get('/api/wines/:id/image', function (req, res) {
145-
// TODO what if image does not exists ?
146-
res.sendFile(__dirname + '/data/images/' + req.params.id + '.png');
147+
var id = req.params.id;
148+
if (!WinesById[id]) {
149+
res.sendStatus(404);
150+
} else {
151+
// TODO what if image does not exists ?
152+
res.sendFile(__dirname + '/data/images/' + req.params.id + '.png');
153+
}
147154
});
148155

149156
/**
@@ -162,9 +169,16 @@ app.get('/api/wines/:id/image', function (req, res) {
162169
* {
163170
* "like": false
164171
* }
172+
*
173+
* @apiError {String} 404 Not found - No wine corresponding to given 'id'
165174
*/
166175
app.get('/api/wines/:id/like', function (req, res) {
167-
res.send({like: Likes.indexOf(req.params.id) >= 0});
176+
var id = req.params.id;
177+
if (!WinesById[id]) {
178+
res.sendStatus(404);
179+
} else {
180+
res.send({like: Likes.indexOf(req.params.id) >= 0});
181+
}
168182
});
169183

170184
/**
@@ -189,9 +203,16 @@ app.get('/api/wines/:id/like', function (req, res) {
189203
* content: "J'ai bu le millésime 2009, parfait après une heure en carafe !"
190204
* }
191205
* ]
206+
*
207+
* @apiError {String} 404 Not found - No wine corresponding to given 'id'
192208
*/
193209
app.get('/api/wines/:id/comments', function (req, res) {
194-
res.send(Comments[req.params.id] || []);
210+
var id = req.params.id;
211+
if (!WinesById[id]) {
212+
res.sendStatus(404);
213+
} else {
214+
res.send(Comments[req.params.id] || []);
215+
}
195216
});
196217

197218
/**

0 commit comments

Comments
 (0)