Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Rest server: refine errors #807

Merged
merged 21 commits into from
Jul 24, 2018
Merged

Conversation

Gerhut
Copy link
Member

@Gerhut Gerhut commented Jul 4, 2018

  • Token error refine is merged.
  • User error refine is merged.
  • VC error refine is merged.
  • Job error refine is merged.
  • README updated.

@Gerhut Gerhut requested review from hwuu, wangcan0329 and abuccts July 4, 2018 05:19
@coveralls
Copy link

coveralls commented Jul 4, 2018

Coverage Status

Coverage decreased (-28.4%) to 51.809% when pulling 29648f7 on qixcheng/rest-server-refine-errors into a97798a on master.

@Gerhut Gerhut force-pushed the qixcheng/rest-server-refine-errors branch from 186f3fe to dcfb91c Compare July 4, 2018 05:25
message: err.message,
error: config.env === 'development' ? err.stack : '',

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?

Copy link
Member Author

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.

@Gerhut Gerhut mentioned this pull request Jul 9, 2018
const httpErrors = require('http-errors');
const statuses = require('statuses');

const createError = exports = module.exports = (status, code, message) => {
Copy link
Contributor

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 and exports?

Copy link
Member Author

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 by const createError = require('error');
  • exports.unknown = () => {} is the general form of export a value as module's field, and can be imported by const 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) => {
Copy link
Contributor

@hwuu hwuu Jul 10, 2018

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 found createError.unknown).

Copy link
Member Author

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.


declare function createError(status: Status, code: Code, message: string): HttpError;
declare namespace createError {
declare function unknown(cause: Error | string): HttpError;
Copy link
Contributor

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?

Copy link
Member Author

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.

Copy link
Contributor

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?

Copy link
Member Author

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.

'Unauthorized';
declare type Code =
'ERR_BAD_CONFIGURATION' |
'ERR_CONFLICT_JOB' |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONFLICT --> CONFLICTED

declare type Code =
'ERR_BAD_CONFIGURATION' |
'ERR_CONFLICT_JOB' |
'ERR_CONFLICT_USER' |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CONFLICT --> CONFLICTED

'Not Found' |
'Unauthorized';
declare type Code =
'ERR_BAD_CONFIGURATION' |
Copy link
Contributor

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?

Copy link
Member Author

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.


declare type Status =
'Bad Request' |
'Conflict' |
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Conflict --> Conflicted

Copy link
Member Author

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

@Gerhut
Copy link
Member Author

Gerhut commented Jul 13, 2018

According to Microsoft REST API Guidelines, all error codes were renamed to CamelCase.

Since no error.code field in current (mainline) version, this change introduces no backward compatibility problems.

* 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.
Gerhut and others added 8 commits July 18, 2018 14:42
* 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
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants