Skip to content

Feature/#909 error codes#957

Closed
StarpTech wants to merge 95 commits intofastify:nextfrom
StarpTech:feature/#909_error_codes
Closed

Feature/#909 error codes#957
StarpTech wants to merge 95 commits intofastify:nextfrom
StarpTech:feature/#909_error_codes

Conversation

@StarpTech
Copy link
Copy Markdown
Member

This is a first draft of introducing error codes to fastify. It only handles the codes for the contentTypeParser.

The old PR discussion is here

TODO

  • Move error message and code to error

Checklist

  • run npm run test and npm run benchmark
  • tests and/or benchmarks are included
  • documentation is changed or added
  • commit message and code follows Code of conduct

@mcollina
Copy link
Copy Markdown
Member

mcollina commented Jun 3, 2018

This is good for me. It needs all errors to be copied and docs.

Copy link
Copy Markdown
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

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

Nice work! I like it.

Only a suggestion

Comment thread lib/errors.js Outdated
return `FastifyError [${code}]`
}
get code () {
return code
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this property set to the prototype?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

FastifyError.prototype.code = code

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Nice!

Copy link
Copy Markdown
Member

@allevo allevo left a comment

Choose a reason for hiding this comment

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

Can you list all errors in the doc?

Comment thread lib/errors.js Outdated

function createMessage (msg, args = []) {
args.unshift(msg)
if (args.length === 0) return msg
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This line should be before args.unshift(msg), otherwise args.length will always be at least 1.

Comment thread lib/errors.js Outdated
code = code.toUpperCase()
codes[code] = class FastifyError extends Base {
constructor (...args) {
super(`Code: ${code}; ${createMessage(msg, args)}`)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

As an alternative to my comment above, you could use util.format() here directly. It already includes a check similar to the one you have in createMessage().

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good addition. I will remove it.

Comment thread lib/errors.js Outdated

function createError (code, msg, statusCode = 500, Base = Error) {
if (!code) throw new Error('Fastify error code must not be empty')
if (!msg) throw new Error('Fastify error message must not be empty')
Copy link
Copy Markdown
Member

@delvedor delvedor Jun 4, 2018

Choose a reason for hiding this comment

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

If you pass an empty string this will throw, is it ok?
Can you use more strict checks?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think that's enough. I just want to avoid that an empty value (0, '') or no value is passed.

Copy link
Copy Markdown
Member

@delvedor delvedor left a comment

Choose a reason for hiding this comment

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

Great work!
Just a question, why are you using the class syntax?

Personally, I prefer to stick classic one, for two main reasons:

  1. in Fastify we aren’t using (es6) classes anywhere, while we are using JavaScript prototype-based inheritance (see request, reply…)
  2. class syntax is just syntactic sugar around the classic JavaScript prototype-based inheritance

Would the following code produce the same result?

const { inherits } = require('util')

function FastifyError (code, message) {
  Error.call(this)
  Error.captureStackTrace(this, FastifyError)
  this.name = `FastifyError [${code}]`
  this.code = code
  this.message = message
}

inherits(FastifyError, Error)

@mcollina
Copy link
Copy Markdown
Member

@StarpTech ping

@StarpTech
Copy link
Copy Markdown
Member Author

Hi @mcollina my time is very limited right now. I will try to continue my work on the weekend. Thanks for pointing me out.

jineshshah36 and others added 4 commits September 16, 2018 20:19
…tify#1162)

* added default Query, Params, Headers, and Body types

* fixed contentTypeParser types

* added additional tests for default Buffer body parser

* fixed tests

* allow array of strings in addContentTypeParser
* added default Query, Params, Headers, and Body types

* added tests

* added tests for generics

* added generics info to docs
* Added error handling doc

* Updated based on comments

* Update 2, based on comments

* Updated based on comments
@mcollina
Copy link
Copy Markdown
Member

There is something off with this branch. You should probably rebase over next.

This reverts commit 4f0dca1, reversing
changes made to d60680e.
@StarpTech
Copy link
Copy Markdown
Member Author

I will create a new one.

@StarpTech StarpTech closed this Sep 18, 2018
@StarpTech StarpTech mentioned this pull request Sep 18, 2018
6 tasks
@github-actions
Copy link
Copy Markdown

This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Feb 14, 2022
@Eomm Eomm added the semver-minor Issue or PR that should land as semver minor label Apr 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

discussion Issues or PRs with this label will never stale documentation Improvements or additions to documentation semver-major Issue or PR that should land as semver major semver-minor Issue or PR that should land as semver minor

Projects

None yet

Development

Successfully merging this pull request may close these issues.