-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherrors.js
85 lines (78 loc) · 2.66 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
class PluginError extends Error {
/**
* @param {string} err_type
* @param {string} msg
*/
constructor(err_type, msg) {
super(`[${err_type}] ${msg} `);
this.err_type = err_type;
this.msg = msg;
}
}
export class MessageParseErr extends PluginError {
/**
* @param {string} parse_err_type
* @param {string} details
* @param {Object} part
* @param {Object} orig_msg
*/
constructor(parse_err_type, details, part, orig_msg) {
super("MsgParseErr", `${parse_err_type}: ${details}, examing part=${JSON.stringify(part)} in msg=${JSON.stringify(orig_msg)}`);
this.parse_err_type = parse_err_type;
this.details = details;
this.part = part;
this.orig_msg = orig_msg;
}
}
export class FieldMissingErr extends MessageParseErr {
/**
* @function constructor
* @param {string} field_name
* @param {Object} part
* @param {Object} orig_msg
*/
constructor(field_name, part, orig_msg) {
super("Missing Field",
`field_name="${field_name}"`, part, orig_msg);
this.field_name = field_name;
}
}
export class IllegalFieldTypeErr extends MessageParseErr {
static Type = {
ENUM: 1,
ARRLEN: 2,
OTHER: 0
};
/**
* @function constructor
* @param {number} type
* @param {string} field_name
* @param {string | null} require_type
* @param {Array | null} enum_list
* @param {number | null} arr_len
* @param {Object} part
* @param {Object} orig_msg
*/
constructor(type, field_name, require_type, enum_list, arr_len, part, orig_msg) {
let found_val = part[field_name];
let found_type = typeof(found_val);
if (type === IllegalFieldTypeErr.Type.OTHER) {
super("Illegal Field",
`field_name="${field_name}, require a val in list ${enum_list}, get ${found_val}"`, part, orig_msg);
} else if (type === IllegalFieldTypeErr.Type.ENUM) {
super("Illegal Field",
`field_name="${field_name}", require ${require_type}, get ${found_type}`, part, orig_msg);
} else if (type === IllegalFieldTypeErr.Type.ARRLEN) {
super("Illegal Field",
`field_name="${field_name}, require arr_len=${arr_len}, get arr_len=${found_val.length()}"`, part, orig_msg);
} else {
throw `illegal type specified: ${type}`
}
this.found_type = found_type;
this.found_val = found_val;
this.field_name = field_name;
this.require_type = require_type;
this.enum_list = enum_list;
this.required_arr_len = arr_len;
}
}