-
Notifications
You must be signed in to change notification settings - Fork 549
Conversation
Gerhut
commented
Jul 4, 2018
•
edited
Loading
edited
- Token error refine is merged.
- User error refine is merged.
- VC error refine is merged.
- Job error refine is merged.
- README updated.
186f3fe
to
dcfb91c
Compare
message: err.message, | ||
error: config.env === 'development' ? err.stack : '', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
error [](start = 4, length = 5)
If we removed error here, will it cause client incompatible?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error#stack
, like comment, is listed mostly just for developer use and it is useless for automatic tools, and it has no content in production
environment, so any client compatible with production environment should not depend on this field.
const httpErrors = require('http-errors'); | ||
const statuses = require('statuses'); | ||
|
||
const createError = exports = module.exports = (status, code, message) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 questions here:
- What does
createError = exports = module.exports
mean? - What is the difference between
module.exports
andexports
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since this module will be used by other developers lots of times, I concerned giving more hints to editor to help developer using it.
The simple answer is:
module.exports = () => {}
is the general form of export a value as module itself, and can be imported byconst createError = require('error');
exports.unknown = () => {}
is the general form of export a value as module's field, and can be imported byconst createUnknownError = require('error').unknown;
These two general forms can be easily detected by editors and give helps to autocomplete/intellisense.
When the commonJS module is initialized, module loader will assign an empty object to both exports
and module.exports
, and use the value of module.exports
as module exports.
In other words. exports
is just an alias of module.exports
. When module.exports
is overwritten, exports still reference the original module.exports
object assigned by module loader, which will not be exported. So simply assign value to exports.unknown
will not make the value exported, though it can be detected by editors. After assign the newly exported module.exports
value to exports
, any field assigned to exports
can be exported in normal.
However, since d.ts
is written, these hints only helps editors without supports of TypeScript but able to detect exports of commonJS module. Such as legacy WebStorm. (Didn't use it for a while)
return error; | ||
}; | ||
|
||
const createUnknownError = exports.unknown = (cause) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2 questions here:
- What does
exports.unknown
mean? - Where is
createUnknownError
used? I didn't find other place referring this function (I just foundcreateError.unknown
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
exports.unknown
explained in the previous comment.
createUnknownError
is used in line 36 Error.captureStackTrace
, which helps avoid useless createUnknownError
context output to error.stack.
rest-server/src/util/error.d.ts
Outdated
|
||
declare function createError(status: Status, code: Code, message: string): HttpError; | ||
declare namespace createError { | ||
declare function unknown(cause: Error | string): HttpError; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What does Error | string
mean please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Error | string
means union type: either an Error object or a string.
The upstream error may be an error object by utility library, or an HTTP response text.
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, | ||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this file need to explicitly 'require' error.d.ts
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Editor will find d.ts
with the same filename automatically.
rest-server/src/util/error.d.ts
Outdated
'Unauthorized'; | ||
declare type Code = | ||
'ERR_BAD_CONFIGURATION' | | ||
'ERR_CONFLICT_JOB' | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CONFLICT --> CONFLICTED
rest-server/src/util/error.d.ts
Outdated
declare type Code = | ||
'ERR_BAD_CONFIGURATION' | | ||
'ERR_CONFLICT_JOB' | | ||
'ERR_CONFLICT_USER' | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
CONFLICT --> CONFLICTED
rest-server/src/util/error.d.ts
Outdated
'Not Found' | | ||
'Unauthorized'; | ||
declare type Code = | ||
'ERR_BAD_CONFIGURATION' | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe there is no need to use the ERR_
prefix?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In previous practices, error code is transfered code by code, human by human, and may lost its error means in a high rate. Using a prefix to remind it is an error.
rest-server/src/util/error.d.ts
Outdated
|
||
declare type Status = | ||
'Bad Request' | | ||
'Conflict' | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Conflict --> Conflicted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the standard HTTP response message from https://github.com/jshttp/statuses/blob/master/codes.json#L34
According to Microsoft REST API Guidelines, all error codes were renamed to CamelCase. Since no |
* REST Server: refine user errors. * REST Server: typo * REST Server: move `user` field check in PUT user to param-validation * REST Server: add user test case * REST Server: Rename error codes according to the Mirosoft api-guideline * REST Server: make admin param in add/update user optional.
* REST Server: refine token errors * REST Server: Rename error codes according to the Mirosoft api-guideline
* REST Server: refine vc errors. * REST Server: add vc negative test case * REST Server: Rename error codes according to the Mirosoft api-guideline
* Refine job errors * Add test cases * REST Server: Rename error codes according to the Mirosoft api-guideline
…ror/readme REST Server: update error cases to README