refactor: better error handling for server side calls (#101)

This commit is contained in:
Bereket Engida
2024-10-05 23:35:01 +03:00
committed by GitHub
parent 7cd1985d0e
commit 0ccf68ceed
20 changed files with 269 additions and 530 deletions

View File

@@ -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)
}
}
```