Handle API Errors in Frontend
After this page, you can handle metadata API errors cleanly in your UI.
Problem
Users see confusing failures when a source is missing or access is denied.
Prerequisites
- Frontend HTTP client
- Metadata API integration
Steps
Handle status codes by intent:
403: user is not allowed to access this source404: source key is missing or stale
ts
async function loadSource(sourceKey: string) {
const response = await fetch(`/report-builder/sources/${sourceKey}`);
if (response.status === 403) {
throw new Error('You do not have permission to access this source.');
}
if (response.status === 404) {
throw new Error('This source does not exist anymore. Please refresh.');
}
if (!response.ok) {
throw new Error('Unable to load source metadata right now.');
}
return response.json();
}Verify
- Use one unauthorized user account and confirm
403UI state. - Request a fake source key and confirm
404UI state.
Common mistakes
- Showing generic “Something went wrong” for all cases.
- Treating
404and403the same way. - Depending on server message strings instead of status codes.