question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if youā€™re still stuck at the end, weā€™re happy to hop on a call to see how we can help out.

Throw a json response?

See original GitHub issue

This is not an issue, rather a discussion.

Is there a way to reduce this part (<--) of the code?

if (action === "save") {
  const result = getParams<SaveActionType>(formData, saveSchema);

  if (!result.success) {                            // <--
    return json(result.errors, { status: 400 });    // <--
  }                                                 // <--

  const { id, ...values } = result.data;
  await updateExam(id, values);
}

In fact, my real question is: does a throw in getParams could be captured and transformed, in some way, in the returned json via the CatchBoundary?

If I have several actions, it seems repetitive. Perhaps can it be improved? ā€“Ā Iā€™m fully aware that at some point, things need to be written, just asking ;D

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
kilimancommented, Feb 17, 2022

No. This would actually go to ErrorBoundary. Like Iā€™ve said before, I think itā€™s better to return form validation errors, so you can display the messages.

This is if you want to fail fastā€¦ but Iā€™m not a fan of throwing Error, since that should be reserved for unexpected errors.

0reactions
binajmencommented, Feb 17, 2022

Interesting. To be used in combination with CatchBoundary I suppose? But it means you canā€™t give feedbacks in a form input?

Still, handy to have this version! šŸ‘

Read more comments on GitHub >

github_iconTop Results From Across the Web

How can I pass JSON body of Fetch response to Throw Error ...
I am NOT trying to convert an object into a JSON string. I'm using SweetAlert2 with Laravel. throw error(response => response.json()) swal({Ā ...
Read more >
Response.json() - Web APIs - MDN Web Docs
The json() method of the Response interface takes a Response stream and reads it to completion. It returns a promise which resolves with...
Read more >
JSON error handling
In cases where a JavaScript Object Notation (JSON) transaction fails, the API Gateway can use a JSON error to convey error information to...
Read more >
How to check if an API error response is JSON or not with ...
If the response is ok , I use the Response.json() method to get my data. Otherwise, I throw the response to catch() the...
Read more >
Error while parsing JSON Response in REST API Post call
I have copied and pasted the REST Request and Response JSON definitions into the Request and Response text boxes provided by OS REST...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found