5xx codes you should retry. As the server gave up the request, so retrying after a moment should get the required data.
4xx codes refer to the Documentation for the code meaning and the body error message if present.
Again a 5xx error means the server fluffed up, ie failed DB connection, gave up trying to send a reply, went for tea, got lost in a maze, some other issue thats not apparent.
With a 5xx error, just retry the request