I have a database table which stores Incident details. Each Incident can have an
ID: 639423 • Letter: I
Question
I have a database table which stores Incident details. Each Incident can have an image saved for it. There are some Incidents which will not have an image for it.
I have an API which pulls the image for a given Incident. I have an issue with handling the API in case an Incident doesn't have the image.
What is the best approach to handle this (and why :) )?:
Telling the client in advance which Incident has the image, and which don't. So the client calling the API should only call it when there is an image for the Incident.
Sending a predefined error message as the result.
Sending a generic placeholder like image to the client when an image is not available for the Incident.
Explanation / Answer
I would prefer option 1.
This would involve expanding your current definition of an Incident to contain a hasImage flag so that the client can decide for which Incidents to call the image fetching API (eg, getIncidentImage(...))
Thos covers modelling and API design.
As other people have mentioned, from a UX perspective, you might want to include a placeholder image in the UI when an Incident lack one. If it were up to me, fetching this placeholder image would be done by invoking a getPlaceholderImage() method in your image API and not the getIncidentImage(...) method. This is because, in my opinion, the decision to use a placeholder image when an Incident image is missing is a UI concern and, therefore, your image API shouldn't be aware of it.
Also, as I mentioned in my earlier comment, if it's perfectly normal for an Incident to not have an associated image then I recommend not returning an error when calling your getIncidentImage(...) message. An empty result should be sufficient and is less confusing to the API users.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.