Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error Handling from docs completely not working #2688

Open
KrisLau opened this issue Jul 22, 2024 · 2 comments
Open

Error Handling from docs completely not working #2688

KrisLau opened this issue Jul 22, 2024 · 2 comments

Comments

@KrisLau
Copy link

KrisLau commented Jul 22, 2024

Doc: https://vincit.github.io/objection.js/recipes/error-handling.html#examples

Error I'm testing:

 RequestError: insert into [user] (//columns) output inserted.[id] values (@p0, @p1, @p2, @p3, @p4, @p5, @p6, @p7, @p8, @p9, @p10) - Violation of PRIMARY KEY constraint 'user_pkey'. Cannot insert duplicate key in object 'dbo.user'. The duplicate key value is (//id).
 User.query(trx)
  .insert(created)
  .onError(async (error, _query) => {
    const testErr = errorHandler(error);

    console.log(testErr.type); //always returns UnknownError
    console.log(error.type); //always returns undefined

    // returns false for all
    console.log(error instanceof CheckViolationError);
    console.log(error instanceof ForeignKeyViolationError);
    console.log(error instanceof ConstraintViolationError);
    
    // other code
@kapouer
Copy link
Contributor

kapouer commented Aug 30, 2024

The errorHandler from documentation does not return anything, so testErr would be empty.

Try to log the actual error. You probably missed something.

@KrisLau
Copy link
Author

KrisLau commented Sep 1, 2024

@kapouer I had swapped the res.status.send on all the switch case statements from the documentation example to a return back when I posted this. The testErr wasn't empty if you read the comments I added to my code above:

  console.log(testErr.type); //always returns UnknownError
  console.log(error.type); //always returns undefined

It was returning UnknownError meaning it wasn't matching any of the defined types and was returning the default case.

I dont have the modified code now since I gave up on it but if you look at the code i posted directly checking in the onError also didnt work:

 .onError(async (error, _query) => {
    ...
    // returns false for all
    console.log(error instanceof CheckViolationError);
    console.log(error instanceof ForeignKeyViolationError);
    console.log(error instanceof ConstraintViolationError);

Try to log the actual error. You probably missed something.

The error is logged in the very first code snippet i included in the original post.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants