Skip to content
tezvyn:

Describe a robust error handling strategy for network requests

Source: pub.devMediumHow cards are made

Tests whether you classify failures by layer rather than catching everything generically. Inspect DioExceptionType for connectivity, check HTTP status before parsing, and isolate JSON decode errors. Never show the same message for timeouts and 500s.

What's really being asked

The interviewer wants to see if you think in layers. Network code fails in three distinct places: the transport layer cannot reach the server, the server returns an unhappy HTTP status, or the payload does not match the expected schema. A senior candidate demonstrates that each layer needs its own detection path and user-facing strategy.

The full answer

First, catch DioException and inspect its type field. DioExceptionType.connectionTimeout, receiveTimeout, or connectionError indicate a network connectivity problem, so you should suggest retry logic with exponential backoff and a user message about checking their connection. Second, when type is DioExceptionType.badResponse, examine the Response statusCode. A 404 means the resource is missing, a 401 or 403 means authentication or authorization failed, and a 500 range means a server-side error; each should map to a different domain exception or UI state. Third, after you confirm the status code is successful, parse the response body. Wrap JSON deserialization in its own try-catch because a 200 OK can still contain malformed JSON or an unexpected schema; this isolates serialization bugs from transport bugs. Fourth, mention logging and telemetry. Connectivity errors might be retried silently, 5xx errors should be logged to your crash reporter as warnings, and JSON parse errors should be logged as critical data contract violations.

The mistakes people make

Catching every exception generically and showing a single generic toast message. Treating HTTP 404 or 500 as a network connectivity error and retrying indefinitely. Parsing JSON directly inside the same catch block that handles DioException, which makes it impossible to tell whether the failure came from the wire or from the mapper. Ignoring the response body when the status code is non-2xx, which wastes useful server error messages.

What usually comes next

How would you implement retry with exponential backoff without blocking the UI? What do you do when the server returns 200 but the JSON contains an error field instead of the expected payload? How do you cancel an in-flight request when the user leaves the screen? How would you write unit tests for each failure layer without making real network calls?

A concrete example

Suppose you call dio.get to fetch a user profile. The phone is in airplane mode, so Dio throws DioExceptionType.connectionError; your handler emits a NetworkFailure state and shows an offline banner. Next, you turn off airplane mode but the user ID does not exist; Dio returns a 404 with DioExceptionType.badResponse; your handler emits a NotFoundFailure and shows a profile not found screen. Finally, you request a valid ID, the server returns 200, but the JSON omits the required email field; your JSON mapper throws a FormatException; your handler emits a DataCorruptionFailure and logs the exact payload to your monitoring dashboard so the backend team can fix the contract.

Interview question

Which strategy best distinguishes a transport timeout from a malformed JSON payload when using Dio?

  • a.Treat any non-2xx HTTP status as a connectivity error and retry with exponential backoff
  • b.Catch all exceptions generically and display a single toast message
  • c.Check DioExceptionType for connectivity issues, then parse JSON in a separate try-catch after confirming HTTP successCorrect
  • d.Inspect the exception message string for timeout or format keywords
Why?

The correct answer reflects the layered approach: inspect DioExceptionType for transport failures first, then isolate JSON deserialization in its own try-catch so schema mismatches are not confused with wire errors. Option B is wrong because a generic catch block makes it impossible to tell whether the failure came from the network or the mapper.

Just read this? Test yourself on what you have been reading.

Read the original → pub.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on flutter — each one lists the topics its interview covers.

See open roles