Error Handling
Typed errors with type guards. Catch by type, not by string matching.
Error Hierarchy
All errors extend GlydeError, which extends native Error:
// Error hierarchy
GlydeError (base)
├── HttpError — non-2xx response (has status, response, config)
├── TimeoutError — request exceeded timeout
└── NetworkError — fetch failed (DNS, offline, CORS)Usage
import { isHttpError, isTimeoutError, isGlydeError } from "glyde"
try {
await api.get("/data")
} catch (err) {
if (isHttpError(err)) {
console.log(err.status) // 404
console.log(err.response?.data) // parsed response body
console.log(err.config.url) // "/data"
console.log(err.message) // "HTTP 404: Not Found"
}
if (isTimeoutError(err)) {
// Request was aborted after timeout
console.log(err.message) // "Request timed out after 5000ms"
}
if (isGlydeError(err)) {
// Catches ALL glyde errors (network, timeout, http)
}
}HttpError
Thrown when the server responds with a non-2xx status. Contains the full response:
class HttpError extends GlydeError {
status: number // e.g. 404
response?: GlydeResponse // full response with parsed data
config: RequestConfig // what was requested
message: string // "HTTP 404: Not Found"
}Type Guards
Use type guards instead of instanceof — they work across module boundaries and provide proper TypeScript narrowing.
// Type guards work across module boundaries (safer than instanceof)
import { isGlydeError, isHttpError, isTimeoutError } from "glyde"
// Each narrows the type:
if (isHttpError(err)) {
err.status // TypeScript knows this exists
err.response.data // TypeScript knows this exists
}Example: Retry on 429
// Retry pattern with interceptors
api.interceptors.response.use(
(response) => response,
async (error) => {
if (isHttpError(error) && error.status === 429) {
// Rate limited — wait and retry
await new Promise(r => setTimeout(r, 1000))
return api.request(error.config)
}
throw error
}
)When each error is thrown
| Error | When | Has response? |
|---|---|---|
HttpError | Server returned non-2xx status | Yes — full parsed response |
TimeoutError | Request exceeded configured timeout | No |
NetworkError | fetch() itself failed (DNS, offline, CORS) | No |