binhend 1.1.8 → 1.1.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/api.js +11 -11
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "binhend",
3
- "version": "1.1.8",
3
+ "version": "1.1.10",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "author": "Nguyen Duc Binh",
package/src/api.js CHANGED
@@ -86,21 +86,21 @@ function trycatch(callback, onerror) {
86
86
  return await callback(request, response, next);
87
87
  }
88
88
  catch (error) {
89
- if (onerror instanceof Function) onerror(error);
90
- else {
91
- var info = typeof error.info === 'string' ? { error: error.info } : error.info;
92
- response.status(error.httpCode || 500).json(info || { error: 'Internal Server Error' });
93
- }
89
+ if (onerror instanceof Function) return onerror(error);
90
+ var message = error instanceof HttpError && error.message || 'Internal Server Error';
91
+ response.status(error.code || 500).json({ error: message });
92
+ if (onerror === true) console.error('\x1b[31m', error);
94
93
  }
95
94
  }
96
95
  }
97
96
 
98
- function HttpError(httpCode, info) {
99
- var error = new Error(info);
100
- error.httpCode = httpCode;
101
- error.info = info;
102
- return error;
103
- };
97
+ class HttpError extends Error {
98
+ constructor(code, message) {
99
+ super(message);
100
+ this.name = 'HttpError';
101
+ this.code = code;
102
+ }
103
+ }
104
104
 
105
105
  module.exports = {
106
106
  APIs, Router, HttpError