Right now Frontity doesn’t return proper status codes for 4xx and 5xx REST API errors. All REST API errors become a 404, which is wrong.
The first step to solve this is to populate data with the correct information from the REST API, so the Frontity app can make decisions based on that info.
User Stories
As a Frontity developer
I want to know about status codes and errors from the REST API
so that I can make decisions of what to show in the front-end
Possible solution
If there is an error in actions.source.fetch we have to populate the data object with info about that error.
REST API errors: When we catch the error, we use that info to populate data.
In order to catch the fetch errors, you need to see if the response was ok and if not, throw the response as the error object:
const ErrorPage = ({ state }) => {
const data = state.source.get(state.router.link);
if (data.is404) return <div>Ops, that page can't be found.</div>;
return <div>Ops! Something went wrong.</div>;
}
The theme needs to inform the user if either the page doesn’t exist or something else happened.
Of course this is only to show people how it works, they can implement it however they want.
I think that in the context of a client-side web application it’s a better user experience if we allow the user to stay on the current page if there is a problem with fetching data for the next page, rather than redirect to an “Error Page”.
So, for example:
User is on the archive page.
Clicks on the link to a post The Beauties of Gullfoss
The data fails to load
a) we redirect the user to an error page b) we show a toast notification saying something went wrong and the user stays on the archive page.
I think b) is better
I’m not sure if this is somehow more difficult to implement but just looking at the code it should not be.
Oh, I see. It could be a good addition but we must provide a) for server-side rendering. I mean, we cannot assume that we are going to always load a page without errors in the first place.
With the new implementation, if calling get() has returned a status code higher than 400, we add information about the error to the state. For example, if an error code was 500 , the state will include the following properties:
{
isError: true,
is500: true,
errorStatus: 500,
errorStatusText: "Some string describing the error",
// booleans that describe the fetch status
isReady: true,
isFetching: false
}
@SantosGuillamot We should find a way to mark feature discussions as “done” once they are implemented. I don’t know if it makes sense to close the topic or just edit the title and add a [Released] at the beginning. What do you think?
I was working on adding the status after the title, precisely to make it more visible as you say. I implemented it but after an update it was breaking the private messages. I have to take a look at it but I didn’t have time yet. This was the initial idea: