I have developed several frontend applications and as I analyse I often need to refactor the front-end code or frontend crash in the following case:
Let's assume there is a endpoint /movies
that returns the following response:
{
"movieName": "Avengers Endgame",
"genre": "Action, Thriller",
"length": 120 // in minutes
"rating": 4,
"id": 1
}
To handle this endpoint's response in the frontend application (React + Typescript), I have created a MovieType
and will render the data in the component.
// types.ts
export type MovieType = {
movieName: string;
genre: string;
length: number;
rating: number;
id: number;
}
import React from 'react';
function MovieComponent() {
const [movies, setMovies] = React.useState([] as MovieType[]);
React.useEffect(() => {
const data:MovieType[] = fetchDataSomeHow() as MovieType[];
setMovies(data);
})
return (
<ul>
movies.map(movie => (
<li key={movie.id}>
{movie.movieName.toLoweCase()} - {movie.rating}
</li>
))
</ul>
)
}
So here I have created MovieType
by looking at the response of the /movies
endpoint and marked movieName
as required in MovieType
. Here the MovieType
is affected by the data returned by the /movies
endpoint.
As I am using Typescript and I have defined movieName
as string
and required
, I can use toLowerCase
method without any additional checks.
Now let's suppose that there is some bad input in the movie database and in the response movieName
is undefined/null
or movieName
is changed to name
. Now the frontend application will crash because of runtime errors.
This happens whenever there is an API contract mismatch in the frontend and backend because of bad database entries or sudden backend API changes.
is there any standard or a good way to handle this kind of errors? I can easily make every field optional and then write frontend code but it will be messy and nonreadble. On the backend side, we have concepts of adapters that validate and sanitise the data and throws errors in case of invalid requests but on the frontend side, we can't hang users on an error page just because we got one field wrong in one object among 1000 objects.