usage.js
3.03 KB
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
var errors = require('../..');
// barebones
errors.create({name: 'RuntimeError'});
console.log(new errors.RuntimeError().toString());
console.log("-------------");
// default message
errors.create({
name: 'RuntimeError',
defaultMessage: 'A runtime error occured during processing'
});
console.log(new errors.RuntimeError().toString());
console.log("-------------");
// default message, explanation and response
errors.create({
name: 'FileNotFoundError',
defaultMessage: 'The requested file could not be found',
defaultExplanation: 'The file /home/boden/foo could not be found',
defaultResponse: 'Verify the file exists and retry the operation'
});
console.log(new errors.FileNotFoundError().toString());
console.log("-------------");
// override messages
console.log(new errors.FileNotFoundError(
'Cannot read file'
, 'You do not have access to read /root/foo'
, 'Request a file you have permissions to access').toString());
console.log("-------------");
// define code
errors.create({
name: 'SecurityError',
code: 1100
});
console.log(new errors.SecurityError().toString());
console.log("-------------");
// inheritance
errors.create({
name: 'FatalError',
defaultMessage: 'A fatal error occurred',
});
errors.create({
name: 'FatalSecurityError',
defaultMessage: 'A security error occurred, the app must exit',
parent: errors.FatalError
});
try {
throw new errors.FatalSecurityError();
} catch (e) {
if (e instanceof errors.FatalError) {
// exit
console.log("Application is shutting down...");
}
}
console.log("-------------");
// namespace
errors.create({
name: 'MalformedExpressionError',
scope: exports
});
console.log(new exports.MalformedExpressionError().toString());
console.log("-------------");
// lookup
if (errors.find(1100) === errors.find('SecurityError')) {
console.log("Found em");
}
console.log("-------------");
// mappers
errors.create({name: 'InvalidUsernameError'});
errors.create({name: 'InvalidPasswordError'});
errors.mapper(['InvalidUsernameError', 'InvalidPasswordError'], function(err) {
return new errors.SecurityError('Invalid credentials supplied');
});
console.log(errors.mapError(new errors.InvalidUsernameError()).toString());
console.log(errors.mapError(new errors.InvalidPasswordError()).toString());
console.log("-------------");
// stacks
errors.stacks(true);
console.log(new errors.Http413Error().toString());
console.log(new errors.Http413Error().toJSON());
console.log("---------------");
// options style constructor
errors.create({name:'IdentifiableError'});
console.log(new errors.IdentifiableError({
message: "Unexpected error",
refID: "a1b2c3",
status: 500}).toString());
console.log(new errors.IdentifiableError({
message: "Unexpected error",
refID: "a1b2c3",
status: 500}).toJSON());
console.log("---------------");
// override status in proto
errors.create({name: 'CustomHttpError', status: 488});
console.log(new errors.CustomHttpError().toString());
console.log(new errors.CustomHttpError().toJSON());