First, a code example:
import { ViewModel } from './api';
export const action = async ({ request, context }) => {
const response = await ViewModel({ request, context });
return json(response);
};
Inside of the ViewModel we do things: you know, extract data, validate data, etc. etc. Our validation code is wrapped in a try/catch where we throw an Error if some data doesn't validate. Inside of our catch block we return a new Response() with all of the appropriate settings (content type header json, status: 422 etc). However, it doesn't propagate to the action. The action is sending back a 200. When we copy/paste the new Response() directly into the action everything works as expected.
Context:
- Node
- Express
- Remix
^1.6.3
Questions:
- Is this a JS issue? Am I misunderstanding scope in Node.js or some other concept (async stuff)? I would think that the new Response returned from our View Model would be as if it was thrown within the
action - Should we instead surface errors from our View Model to our action/loaders? In other words, we could return from our View Model an object description of the result and then from the action/loader throw a new response.
Thanks in advance