-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.js
63 lines (56 loc) · 1.45 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
"use strict";
/* eslint-disable new-cap, eqeqeq */
const {errno} = require("errno");
const {ffi, int, pick_one} = require("./util");
class ErrorResolverBase {
resolve(err) {
if (err === -1) {
return {code: "EGENERIC", description: ""};
}
try {
return this._resolve(err);
}
catch (ex) {
return this._resolve_generic();
}
}
_resolve_generic(err) {
const {code = "ERROR", description = ""} = err ? errno[err] || {} : {};
return {code, description};
}
}
class ErrorResolverUV extends ErrorResolverBase {
constructor(libname) {
super();
Object.assign(this, ffi.Library(libname, {
uv_strerror: ["string", [int]],
uv_err_name: ["string", [int]],
uv_translate_sys_error: [int, [int]],
}));
if (!this.uv_strerror) {
throw new Error("No UV");
}
}
_resolve(err) {
const uverr = this.uv_translate_sys_error(err);
return {
code: this.uv_err_name(uverr),
description: this.uv_strerror(uverr),
};
}
}
const resolve_error = pick_one("resolve", "resolve_error", [
[null, ErrorResolverUV],
["libuv", ErrorResolverUV],
["uv", ErrorResolverUV],
[null, ErrorResolverBase],
]);
class FallocateError extends Error {
constructor(err, message) {
const {code = "ERROR", description = ""} = resolve_error(err);
super(`${description || message} (${err}, ${code})`);
this.code = code;
this.errno = err;
}
}
module.exports = { FallocateError };