Skip to content

Conversation

@StarpTech
Copy link
Member

@StarpTech StarpTech commented Sep 18, 2018

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

TODO

  • Create error codes for all others
  • Document the error codes

Previous PR: #957

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

t.strictDeepEqual(JSON.parse(body.toString()), {
statusCode: 413,
statusCode: expectedError.statusCode,
code: expectedError.code,
Copy link
Member

Choose a reason for hiding this comment

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

I think we should keep those explicit in the tests

t.fail()
} catch (err) {
t.is(err.message, 'The content type should be a string')
t.is(err.message, new FST_ERR_CTP_INVALID_TYPE().message)
Copy link
Member

Choose a reason for hiding this comment

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

I would prefer for the message to be explicit here.

Copy link
Member Author

Choose a reason for hiding this comment

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

What's the benefit? I think the error name is already very explicit. In that way we can avoid modifying all tests when we change something in the future.

Copy link
Member

Choose a reason for hiding this comment

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

Sure, but I would love to avoid testing all errors individually.

@delvedor delvedor added the semver-major Issue or PR that should land as semver major label Sep 19, 2018
@delvedor delvedor added this to the v2.0.0 milestone Sep 19, 2018
@stale
Copy link

stale bot commented Oct 4, 2018

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale Issue or pr with more than 15 days of inactivity. label Oct 4, 2018
@cemremengu cemremengu removed the stale Issue or pr with more than 15 days of inactivity. label Oct 4, 2018
@mcollina
Copy link
Member

mcollina commented Oct 4, 2018

@StarpTech any updates here?

@StarpTech
Copy link
Member Author

I will work on it in smaller updates I'm too busy right now. Any help is welcome.

@cemremengu
Copy link
Contributor

@StarpTech happy to help, what's left to do can you provide some kind of check list?

@StarpTech
Copy link
Member Author

@cemremengu sure

  • Have a look at how we want to use errors in the future.
  • Address @mcollina point
  • Change it everywhere & update tests
  • Document the new errors

@cemremengu
Copy link
Contributor

cemremengu commented Oct 5, 2018

@StarpTech Reverted test to use string messages. It would look like this:

      t.strictDeepEqual(JSON.parse(body.toString()), {
        statusCode: 413,
        code: "FST_ERR_CTP_BODY_TOO_LARGE",
        error: 'Payload Too Large',
        message: 'FST_ERR_CTP_BODY_TOO_LARGE: Request body is too large'
      })

Should I push to your branch with your permission? How should we progress?

EDIT:
do these error messages apply to assertions as well or we leave them as is?

@mcollina
Copy link
Member

mcollina commented Oct 5, 2018

I think you can push here, just not force push.

@cemremengu
Copy link
Contributor

cemremengu commented Oct 5, 2018

EDIT: Found it, changing this

    // if we have failed because `resolve has thrown
    // let's rethrow the error and let avvio handle it
    if (err.code === 404) throw err
    return swapSchema

to this

    // if we have failed because `resolve has thrown
    // let's rethrow the error and let avvio handle it
    if (err.statusCode === 404) throw err
    return swapSchema

will solve the problem. Maybe we can match for the real code /FST_ERR_SCHEMA/ (I prefer this) What do you think?

--

I think there is problem with schema.js. The test below fails:

test('Should throw of the schema does not exists', t => {
  t.plan(1)
  const fastify = Fastify()

  fastify.route({
    method: 'GET',
    url: '/:id',
    schema: {
      params: 'test#'
    },
    handler: (req, reply) => {
      reply.send(typeof req.params.id)
    }
  })

  fastify.ready(err => {
    t.is(err.message, 'Schema with id \'test\' does not exist!')
  })
})

If I change/delete the error.code below (since we now have string codes like FST....) in shared-schemas.test.js, ajv will complain that schema must be a an object or boolean

Schemas.prototype.resolve = function (id) {
  if (this.store[id] === undefined) {
    const error = new Error(`Schema with id '${id}' does not exist!`)
    error.code = 404
    throw error
  }
  return this.store[id]
}

Any ideas?

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

Good work so far, from a testing perspective, we need to verify both the code and the message. Sorry if I was not clear before.


function noop () {}
function noop () { }

Copy link
Member

Choose a reason for hiding this comment

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

why this change?

server.add = decorator.add
return server
function server () {}
function server () { }
Copy link
Member

Choose a reason for hiding this comment

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

why this change?

function Instance () {}
Instance.prototype.test = () => {}
function Instance () { }
Instance.prototype.test = () => { }
Copy link
Member

Choose a reason for hiding this comment

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

more unneeded changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

standard --fix or vscode formatter is changing these I think. I will revert them

Copy link
Member

Choose a reason for hiding this comment

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

I think vscode formatter, standard --fix is run every time.

t.fail()
} catch (e) {
t.ok(/missing dependency/.test(e.message))
t.ok(/FST_ERR_DEC_MISSING_DEPENDENCY/.test(e.message))
Copy link
Member

Choose a reason for hiding this comment

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

I think we colluld avoid the regexp here

@mcollina
Copy link
Member

mcollina commented Oct 8, 2018

Very good so far. What is left to do? Docs for certain.

@cemremengu
Copy link
Contributor

Yes, looks like only docs are outstanding. Will send them in today.

@cemremengu
Copy link
Contributor

@StarpTech @mcollina I think this should be good to go after some final polishing (if necessary).

Let me know if anything else needs to be done.

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

LGTM with a couple of nits.

@mcollina mcollina requested review from delvedor and jsumners October 8, 2018 16:43
@mcollina
Copy link
Member

mcollina commented Oct 8, 2018

This PR needs to be merged by hand to keep the dual authorship of the work.

@cemremengu
Copy link
Contributor

@mcollina I personally don't mind and wouldn't want anyone to waste time on that. All credits goes to @StarpTech, he did most of the work anyways.

@StarpTech
Copy link
Member Author

Good work @cemremengu! I also don't mind. Take the easiest way.

docs/Errros.md Outdated
<a id="FST_ERR_CTP_INVALID_PARSE_TYPE"></a>
### FST_ERR_CTP_INVALID_PARSE_TYPE

The provided parse type is not supported. Accepted values are 'string' or 'buffer'.
Copy link
Member Author

Choose a reason for hiding this comment

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

Could you mark the types in "`" as well?

docs/Errros.md Outdated
<a id="FST_ERR_SCH_ALREADY_PRESENT"></a>
### FST_ERR_SCH_ALREADY_PRESENT

A schema with the same `id` already exists.
Copy link
Member Author

Choose a reason for hiding this comment

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

Be consistent use id or $id.

docs/Errros.md Outdated
### FST_ERR_REP_INVALID_PAYLOAD_TYPE

Reply payload can either be a `string` or a `Buffer`.
Reply payload can either be a `'string'` or a `'Buffer'`.
Copy link
Member Author

Choose a reason for hiding this comment

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

You mean without quotes right? 😄

Copy link
Contributor

Choose a reason for hiding this comment

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

Hang on almost there 😄

Copy link
Member Author

@StarpTech StarpTech left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
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.

LGTM

@StarpTech
Copy link
Member Author

Ready to merge?

@mcollina
Copy link
Member

mcollina commented Oct 12, 2018 via email

@StarpTech
Copy link
Member Author

Thank you @cemremengu for takeover.

@StarpTech StarpTech merged commit 2148dbc into fastify:next Oct 12, 2018
@mcollina mcollina mentioned this pull request Dec 20, 2018
2 tasks
@fralonra fralonra mentioned this pull request Sep 12, 2019
4 tasks
@github-actions
Copy link

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 13, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

semver-major Issue or PR that should land as semver major

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants