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

Specify ErrorResponse as interface to provide obvious contract #10876

Merged
merged 5 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/error-response-type.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@remix-run/router": patch
---

Fix `ErrorResponse` type to avoid leaking internal field
1 change: 1 addition & 0 deletions contributors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,4 @@
- yionr
- yuleicul
- zheng-chuang
- sgrishchenko
17 changes: 11 additions & 6 deletions packages/router/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1533,11 +1533,21 @@ export const redirectDocument: RedirectFunction = (url, init) => {
return response;
};

export type ErrorResponse = {
status: number;
statusText: string;
data: any;
};

/**
* @private
* Utility class we use to hold auto-unwrapped 4xx/5xx Response bodies
*
* We don't export the class for public use since it's an implementation
* detail, but we export the interface above so folks can build their own
* abstractions around instances via isRouteErrorResponse()
*/
export class ErrorResponseImpl {
export class ErrorResponseImpl implements ErrorResponse {
status: number;
statusText: string;
data: any;
Expand All @@ -1562,11 +1572,6 @@ export class ErrorResponseImpl {
}
}

// We don't want the class exported since usage of it at runtime is an
// implementation detail, but we do want to export the shape so folks can
// build their own abstractions around instances via isRouteErrorResponse()
export type ErrorResponse = InstanceType<typeof ErrorResponseImpl>;

/**
* Check if the given error is an ErrorResponse generated from a 4xx/5xx
* Response thrown from an action/loader
Expand Down