js-http-errors
Best Practices for HTTP API Error Handling
-
support richer error message with custom 5-digit ErrorNumber
-
auto detect standard HTTP Status code
-
auto generate random request_id
-
JSON-API Response:
responseData:
{ <int:code>, <str:error> ,<str:request_id> }
responseDataV2:
{ <obj:error> , <str:request_id> }
installation
require nodejs > v7.6.0
for class extends support
npm install js-http-errors
parameters
- @status :int:alias=
status_code
: HTTP返回状态码,默认值400 - @message :str:alias=
error_msg
: 错误信息 - @errno :int:alias=
code
: 错误码, 默认值为 40000- 建议使用 5 位数,其中前 3 位应设计等于 http status code 返回状态码
- @trackError:object: 原始错误Error,默认值为 null
usage sample
const http_errors = ; // 0. support init Error by `new ApiError` || `ApiError.new` || `apiError` let e1 = 40100;let e2 = 401;let e3 = errno:40102 message:"require re-login"; // or let e4 = http_errorsApiError;let e5 = http_errorsApiError;let e6 = http_errorsApiError; // orlet e7 = http_errors;let e8 = http_errors; // 1. json response data with random request_idconsole;/*{ code: 40100, error: 'User Require Login', request_id: 'X6822759T8242827'}*/ console;/*{ request_id: 'X6822759T5B7U3TR', error: { name: 'ApiError', errno: 40100, message: 'Unauthorized', trackError: null }}*/ // 2. suggest to use `apiError` or `apiError.new`, avoid to renew ApiError duplicated let e9 = http_errors;let e10 = http_errors; // e9 === e10 , while e9 !== new http_errors.ApiError(e9) // 3. throw Error immediately by `abort`http_errors; // equal to: `http_errors.abort(40100);` let err = "Local Error"http_errors; // equal to: `http_errors.abort({status:403, trackError:err});`