1
1
'use strict' ;
2
- /* globals module */
3
2
4
- var translator = require . main . require ( './src/translator' ) ,
5
- isLanguageKey = / ^ \[ \[ [ \w . \- _ : ] + ] ] $ / ,
3
+ var translator = require . main . require ( './src/translator' ) ;
4
+ var isLanguageKey = / ^ \[ \[ [ \w . \- _ : ] + ] ] $ / ;
6
5
7
- ErrorHandler = { } ;
6
+ var ErrorHandler = { } ;
8
7
9
- ErrorHandler . respond = function ( status , res ) {
8
+ ErrorHandler . respond = function ( status , res ) {
10
9
var errorPayload = ErrorHandler . generate ( status ) ;
11
10
12
11
res . status ( status ) . json ( errorPayload ) ;
13
12
return true ;
14
13
} ;
15
14
16
- ErrorHandler . handle = function ( err , res , payload ) {
15
+ ErrorHandler . handle = function ( err , res , payload ) {
17
16
if ( err ) {
18
17
if ( isLanguageKey . test ( err . message ) ) {
19
- translator . translate ( err . message , 'en-GB' , function ( translated ) {
18
+ translator . translate ( err . message , 'en-GB' , function ( translated ) {
20
19
res . status ( 400 ) . json ( ErrorHandler . generate ( 400 , undefined , translated ) ) ;
21
20
} ) ;
22
21
} else {
@@ -25,12 +24,12 @@ ErrorHandler.handle = function(err, res, payload) {
25
24
} else {
26
25
res . status ( 200 ) . json ( {
27
26
code : 'ok' ,
28
- payload : payload || { }
27
+ payload : payload || { } ,
29
28
} ) ;
30
29
}
31
30
} ;
32
31
33
- ErrorHandler . generate = function ( status , code , message , params ) {
32
+ ErrorHandler . generate = function ( status , code , message , params ) {
34
33
// All arguments are optional
35
34
var errorPayload = ErrorHandler . statusToCode ( status ) ;
36
35
@@ -41,41 +40,41 @@ ErrorHandler.generate = function(status, code, message, params) {
41
40
return errorPayload ;
42
41
} ;
43
42
44
- ErrorHandler . statusToCode = function ( status ) {
43
+ ErrorHandler . statusToCode = function ( status ) {
45
44
var payload = {
46
- code : 'internal-server-error' ,
47
- message : 'An unexpected error was encountered while attempting to service your request.' ,
48
- params : { }
49
- } ;
50
-
51
- switch ( status ) {
52
- case 400 :
53
- payload . code = 'bad-request' ;
54
- payload . message = 'Something was wrong with the request payload you passed in.' ;
55
- break ;
56
-
57
- case 401 :
58
- payload . code = 'not-authorised' ;
59
- payload . message = 'A valid login session was not found. Please log in and try again.' ;
60
- break ;
61
-
62
- case 403 :
63
- payload . code = 'forbidden' ;
64
- payload . message = 'You are not authorised to make this call' ;
65
- break ;
66
-
67
- case 404 :
68
- payload . code = 'not-found' ;
69
- payload . message = 'Invalid API call' ;
70
- break ;
71
-
72
- case 426 :
73
- payload . code = 'upgrade-required' ;
74
- payload . message = 'HTTPS is required for requests to the write api, please re-send your request via HTTPS' ;
75
- break ;
45
+ code : 'internal-server-error' ,
46
+ message : 'An unexpected error was encountered while attempting to service your request.' ,
47
+ params : { } ,
48
+ } ;
49
+
50
+ switch ( status ) {
51
+ case 400 :
52
+ payload . code = 'bad-request' ;
53
+ payload . message = 'Something was wrong with the request payload you passed in.' ;
54
+ break ;
55
+
56
+ case 401 :
57
+ payload . code = 'not-authorised' ;
58
+ payload . message = 'A valid login session was not found. Please log in and try again.' ;
59
+ break ;
60
+
61
+ case 403 :
62
+ payload . code = 'forbidden' ;
63
+ payload . message = 'You are not authorised to make this call' ;
64
+ break ;
65
+
66
+ case 404 :
67
+ payload . code = 'not-found' ;
68
+ payload . message = 'Invalid API call' ;
69
+ break ;
70
+
71
+ case 426 :
72
+ payload . code = 'upgrade-required' ;
73
+ payload . message = 'HTTPS is required for requests to the write api, please re-send your request via HTTPS' ;
74
+ break ;
76
75
}
77
76
78
77
return payload ;
79
78
} ;
80
79
81
- module . exports = ErrorHandler ;
80
+ module . exports = ErrorHandler ;
0 commit comments