Guide 2.16: Developer Guide

Handling Empty Results

When building complex queries with multiple filters, it is entirely possible to request a combination that does not exist in our database. For example, if you request JAMB Computer Studies questions from the year 2002, you will likely get no results because Computer Studies was introduced much later.

The 404 Status Code

2.16.1

Instead of returning an empty array, SdashAPI strictly adheres to RESTful conventions. If no questions match your specific filters, the server will respond with an HTTP 404 Not Found status code and a JSON object containing an error message.

{
  "status": 404,
  "message": "No questions matched your filters, or unknown endpoint."
}

Checking the Response Status

2.16.2

In your frontend code, you must never blindly attempt to access data.question without first verifying the status. Attempting to iterate over a non-existent data array will crash your application.

const response = await fetch(url);
const json = await response.json();

if (json.status === 200) {
  // Safe to render questions
  renderQuiz(json.data);
} else if (json.status === 404) {
  // Handle empty result gracefully
  showEmptyStateUI("No questions found for this specific year. Try another year.");
} else {
  // Handle server errors
  showErrorUI("An unexpected error occurred.");
}

Designing Empty States

2.16.3

Good user experience dictates that you should anticipate these scenarios. Instead of showing a generic error alert, design a friendly "Empty State" component in your app. Offer the user a button to reset their filters, or suggest a different exam year where data is known to be abundant.

What about other errors?

Next, we will look at how to handle API errors and rate limits.

Next Guide: Handling API Errors →
Avatar

How can we help?

We reply immediately

Hello! 👋 How can we help you today?