mirror of
https://github.com/LukeHagar/better-auth.git
synced 2025-12-09 20:27:44 +00:00
refactor: better error handling for server side calls (#101)
This commit is contained in:
@@ -42,3 +42,24 @@ await auth.api.getSession({
|
||||
|
||||
Unlike the client, the server needs the values to be passed as an object with the key `body` for the body, `headers` for the headers, and `query` for the query.
|
||||
|
||||
|
||||
## Error Handling
|
||||
|
||||
When you call an API endpoint in the server, it will throw an error if the request fails. You can catch the error and handle it as you see fit. The error instance is an instance of `APIError`.
|
||||
|
||||
```ts title="server.ts"
|
||||
import { APIError } from "better-auth/api";
|
||||
|
||||
try {
|
||||
await auth.api.signInEmail({
|
||||
body: {
|
||||
email: "",
|
||||
password: ""
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
if (error instanceof APIError) {
|
||||
console.log(error.message, error.status)
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Reference in New Issue
Block a user