This repository has been archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
/
errors.js
150 lines (127 loc) · 4.02 KB
/
errors.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
define([
'underscore',
'lib/strings'
], function (_, Strings) {
'use strict';
return {
/**
* Find an error in this.ERRORS. Searches by string, number,
* or if searchFor contains errno, the errno.
*/
find: function (searchFor) {
var found;
if (typeof searchFor.errno === 'number') {
found = this.find(searchFor.errno);
} else if (typeof searchFor === 'number') {
found = _.find(this.ERRORS, function (value) {
return value.errno === searchFor;
});
} else if (typeof searchFor === 'string') {
found = this.ERRORS[searchFor];
}
return found;
},
/**
* Convert an error, a numeric code or string type to a message
*/
toMessage: function (err) {
if (! err) {
// No error, assume no response from the backend and
// the service is unavailable.
return this.toMessage('SERVICE_UNAVAILABLE');
} else if (err.forceMessage) {
return err.forceMessage;
} else if (err.message) {
return err.message;
}
// try to convert error to something with an error message
var messageSource = this.find(err);
if (messageSource && messageSource.message) {
return messageSource.message;
}
// the original is not a string, default to unexpected error
if (typeof err !== 'string') {
return this.toMessage('UNEXPECTED_ERROR');
}
// The original was already a string, just return it.
return err;
},
/**
* Convert an error, a numeric code or string type to
* a translated message. If a translator is passed in,
* the message will be translated.
*/
toInterpolatedMessage: function (err, translator) {
var msg = this.toMessage(err);
var interpolationContext = this.toInterpolationContext(err);
if (translator) {
return translator.get(msg, interpolationContext);
}
return Strings.interpolate(msg, interpolationContext);
},
/**
* Fetch the string interpolation context out of the server error.
*/
toInterpolationContext: function (/*err*/) {
return {};
},
/**
* Convert an error or a text type to a numeric code
*/
toErrno: function (err) {
var errnoSource = this.find(err);
// try to find an error with an errno.
if (errnoSource && errnoSource.errno) {
return errnoSource.errno;
}
// could not find an error with an errno, return the original.
return err;
},
/**
* Synthesize an error of the given type
*
* @param {String || Number || Object} type
* @param {String} [context]
*/
toError: function (type, context) {
var message = this.toMessage(type);
if (! message) {
message = this.toMessage('UNEXPECTED_ERROR');
}
var err = new Error(message);
err.message = message;
err.errno = this.toErrno(type);
err.namespace = this.NAMESPACE;
err.errorModule = this;
if (context) {
err.context = context;
}
return err;
},
/**
* Check if an error is of the given type
*/
is: function (error, type) {
var code = this.toErrno(type);
return error.errno === code;
},
normalizeXHRError: function (xhr) {
if (! xhr || xhr.status === 0) {
return this.toError('SERVICE_UNAVAILABLE');
}
var serverError = xhr.responseJSON;
if (! serverError) {
return this.toError('UNEXPECTED_ERROR');
}
// We need the error to be normalized before being returned.
// We also need to add a code to the normalized error, that
// contains the status code
var normalizedError = this.toError(serverError.errno);
normalizedError.code = serverError.code;
return normalizedError;
}
};
});