Client
Error Handling

Error Handling

feTS Client returns a WHATWG Response (opens in a new tab) object for all requests. You can either use status attribute to handle specific status code or basically use ok attribute to check if the response is successful or not.

Let's say you have a /pet/{id} endpoint that returns a pet object. If the pet is not found, the server will return a 404 status code. You can handle this case like this:

const response = await client['/pet/{id}']({
    params: {
        id: 1
    }
 });
 
if (response.status === 404) {
    console.error('Pet not found');
    return;
}
// or handle any errors
if (!response.ok) {
    console.error('Something went wrong');
    const errorResponse = await response.text();
    console.log(errorResponse);
    return;
}
 
const pet = await response.json();
console.log(`Pet name is ${pet.name}`);

Validation errors with feTS Server

If you are using feTS Server, any request validation errors will be thrown as ClientValidationError and you can handle them like this:

import { ClientValidationError } from 'fets';
 
try {
    const response = await client['/pet/:id']({
        params: {
            id: 1
        }
    });
    if (response.status === 404) {
        console.error('Pet not found');
        return;
    }
    const pet = await response.json();
    console.log(`Pet name is ${pet.name}`);
} catch (error) {
    if (error instanceof ClientValidationError) {
        console.error('Validation error', error.errors);
    }
}