Document and/or standardize how ActionError
and isActionError
behave, so proxy servers can return predictable response errors.
#1212
virtuallyunknown
started this conversation in
Proposal
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Body
Summary
Document and standardize the way
ActionError
andisActionError
error behave, so that proxy servers sitting in front of an Astro server in SSR mode can create/throw predictable errors which can then be safely read on the client.Background & Motivation
When an application is deployed behind a reverse-proxy such as NGINX, the proxy may return errors directly to the client (browser) in certain conditions. Common examples include caching, rate-limiting or request body size limits:
When an action is called on the frontend of our application, the proxy server will intercept the request. If the request is valid and it satisfies our file size and rate limit constraints, the proxy will forward it to our Astro server (the
proxy_pass
directive). But if the request fails, the proxy will bypass Astro server altogether and send a response with a status code,Content-Type: application/json
headers and optionally some JSON payload directly to the client.If this request fails on a proxy level (when it bypasses Astro), we can still inspect the error object, but it's
code
andstatus
will not match those that the proxy server sent.So what do we do now? I studied the source code to discover how Astro creates these errors when actions are called on the frontend.
handleAction
returns adeserializeActionResult
with a type oferror
and more importantly for our use case,body: await rawResult.text()
.deserializeActionResult
tries to parse the body as json, and then returns{ error: ActionError.fromJson(json), data: undefined };
fromJson
is a static method of the ActionError class, which does a conditional check to see if the value passedisActionError
, and if it is, it returns new a instance ofActionError(body)
.So with all that information in hand, all we have to do in order to pass custom errors from our proxy server to our client, and allow them to be correctly interpreted by Astro, is to study how
isActionError
function works:We just need to return an object with a type of
AstroActionError
, and the code needs to match a key defined incodeToStatusMap
Goals
At the moment, the solution described above works, but none of this is standard or documented, and requires digging into source code, so there are no guarantees that the internal logic won't change and cause breakage between releases.
ActionError
andisActionError
error behaves so that developers who intend to use this in non-standard ways can do so in a predictable way.I also realize this use case may be relatively niche, so it’s completely understandable if the Astro team chooses not to spend time on this. If that’s the case, I would still appreciate any insight into the intended direction moving forward, and whether this behavior might be considered (and listed as) a breaking change should the core logic be updated in the future.
Thank you!
Beta Was this translation helpful? Give feedback.
All reactions