-
Notifications
You must be signed in to change notification settings - Fork 327
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Mark R. Florkowski <[email protected]>
- Loading branch information
1 parent
f3640fb
commit a6c969e
Showing
21 changed files
with
474 additions
and
114 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
"@uploadthing/react": minor | ||
"@uploadthing/shared": minor | ||
"uploadthing": minor | ||
--- | ||
|
||
feat: improve errors and add `errorFormatter` option on the backend |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Error Handling | ||
|
||
## Error Formatting | ||
|
||
You can customize the server-side behavior in your API handler's options by | ||
using an error formatter. | ||
|
||
By default, only the error message is returned to the client, to avoid leaking | ||
any sensitive information. You can customize this behavior by specifying the | ||
`errorFormatter` option when you initialize your file route helper. An error | ||
formatter runs on the server and takes the original `UploadThingError`, and | ||
returns a JSON-serializable object. The error also includes a `cause` property | ||
which contains more information about the nature of the error and what caused | ||
the error to throw in the first place. | ||
|
||
For example, if you're using Zod as an input parser, you can return information | ||
of what fields failed validation by checking if the cause is a `ZodError`. Zod | ||
provides a `flatten` method that returns a JSON-serializable object which we can | ||
return to the client. | ||
|
||
```ts filename="server/uploadthing.ts" | ||
import * as z from "zod"; | ||
|
||
import { createUploadthing } from "uploadthing/next"; | ||
import type { FileRouter } from "uploadthing/next"; | ||
|
||
const f = createUploadthing({ | ||
errorFormatter: (err) => { | ||
return { | ||
message: err.message, | ||
zodError: err.cause instanceof z.ZodError ? err.cause.flatten() : null, | ||
}; | ||
}, | ||
}); | ||
|
||
export const uploadRouter = { | ||
withInput: f.input(z.object({ foo: z.string() })), | ||
// ... | ||
} satisfies FileRouter; | ||
``` | ||
|
||
## Catching errors on the client | ||
|
||
You can catch errors on the client by using the `onUploadFailed` property on the | ||
premade components, or the `useUploadthing` hook. You can access the JSON object | ||
that you returned from your error formatter on the `data` property: | ||
|
||
```tsx | ||
<UploadButton | ||
endpoint="withInput" | ||
input={{ foo: userInput }} | ||
onUploadError={(error) => { | ||
console.log("Error: ", error); | ||
const fieldErrors = error.data?.zodError?.fieldErrors; | ||
// ^? typeToFlattenedError | ||
setError(fieldErrors.foo[0] ?? ""); | ||
}} | ||
/> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import type { Json } from "./types"; | ||
|
||
const ERROR_CODES = { | ||
BAD_REQUEST: 400, | ||
NOT_FOUND: 404, | ||
|
||
INTERNAL_SERVER_ERROR: 500, | ||
INTERNAL_CLIENT_ERROR: 500, | ||
URL_GENERATION_FAILED: 500, | ||
UPLOAD_FAILED: 500, | ||
MISSING_ENV: 500, | ||
FILE_LIMIT_EXCEEDED: 500, | ||
} as const; | ||
|
||
type ErrorCode = keyof typeof ERROR_CODES; | ||
|
||
function messageFromUnknown(cause: unknown, fallback?: string) { | ||
if (typeof cause === "string") { | ||
return cause; | ||
} | ||
if (cause instanceof Error) { | ||
return cause.message; | ||
} | ||
if ( | ||
cause && | ||
typeof cause === "object" && | ||
"message" in cause && | ||
typeof cause.message === "string" | ||
) { | ||
return cause.message; | ||
} | ||
return fallback ?? "An unknown error occurred"; | ||
} | ||
|
||
export class UploadThingError< | ||
TShape extends Json = { message: string }, | ||
> extends Error { | ||
public readonly cause?: Error; | ||
public readonly code: ErrorCode; | ||
public readonly data?: TShape; | ||
|
||
constructor(opts: { | ||
code: keyof typeof ERROR_CODES; | ||
message?: string; | ||
cause?: unknown; | ||
data?: TShape; | ||
}) { | ||
const message = opts.message ?? messageFromUnknown(opts.cause, opts.code); | ||
|
||
super(message); | ||
this.code = opts.code; | ||
this.data = opts.data; | ||
|
||
if (opts.cause instanceof Error) { | ||
this.cause = opts.cause; | ||
} else if (opts.cause instanceof Response) { | ||
this.cause = new Error( | ||
`Response ${opts.cause.status} ${opts.cause.statusText}`, | ||
); | ||
} else if (typeof opts.cause === "string") { | ||
this.cause = new Error(opts.cause); | ||
} else { | ||
this.cause = undefined; | ||
} | ||
} | ||
|
||
public static async fromResponse(response: Response) { | ||
const json = (await response.json()) as Json; | ||
const message = | ||
json && | ||
typeof json === "object" && | ||
"message" in json && | ||
typeof json.message === "string" | ||
? json.message | ||
: undefined; | ||
return new UploadThingError({ | ||
message, | ||
code: getErrorTypeFromStatusCode(response.status), | ||
cause: response, | ||
data: json, | ||
}); | ||
} | ||
} | ||
|
||
export function getStatusCodeFromError(error: UploadThingError<any>) { | ||
return ERROR_CODES[error.code] ?? 500; | ||
} | ||
|
||
function getErrorTypeFromStatusCode(statusCode: number): ErrorCode { | ||
for (const [code, status] of Object.entries(ERROR_CODES)) { | ||
if (status === statusCode) { | ||
return code as ErrorCode; | ||
} | ||
} | ||
return "INTERNAL_SERVER_ERROR"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export * from "./types"; | ||
export * from "./utils"; | ||
export * from "./file-types"; | ||
export * from "./error"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
a6c969e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
docs-uploadthing – ./
uploadthing-1m3c.vercel.app
docs-uploadthing-pinglabs.vercel.app
docs-uploadthing-git-main-pinglabs.vercel.app
docs.uploadthing.com