Contentful logo

Contentful Community

How can I check for a resource not found error?

How can I use a try/catch to catch various errors? Specifically Resource Not Found

Hey Junie,

I suggest you wrap your API calls in a try/catch block. You can do it like so:

try {
    $entry = $client->getEntry('<entryId>');
} catch (\Contentful\Exception\NotFoundException $exception) {
    // Your logic here
}

You can replace \Contentful\Exception\NotFoundException with any of the provided exception objects in the \Contentful\Exception namespace (see the available ones on Github.

By using \Contentful\Exception\ApiException you can catch all of the exceptions that are thrown from API errors, so stuff like rate limiting and invalid tokens will be converted into actual exceptions.

1 Like