@void-snippets/client
Error Handling
handleApiError() normalises axios errors into standard Error objects.
#handleApiError(error: unknown): never
Normalises any error thrown by axios into a clean, standard Error object with a human-readable message. All five ResourceService methods call this automatically — you only need to use it directly if you make axios calls outside a service.
| What was caught | What is thrown |
|---|---|
AxiosError with response.data.message | new Error(serverMessage) — the message your API sent |
AxiosError without a server message | new Error(axiosError.message) — the axios network message |
A standard Error | Re-throws the same error unchanged |
| Anything else (string, object, …) | new Error("An unexpected error occurred.") |
This function always throws — it never returns. TypeScript's return type is
never.
#Using it directly
typescript
import axios from 'axios';
import { handleApiError } from '@void-snippets/client';
async function uploadFile(file: File) {
const formData = new FormData();
formData.append('file', file);
try {
const res = await axios.post('/upload', formData);
return res.data;
} catch (err) {
handleApiError(err); // always throws a clean Error — never returns
}
}