Advanced 15 min read

Loading States, Errors, and Timeouts in Integrations

By the end of this lesson, you'll be able to:

  • Recognize the additional ways an external callout can fail compared to an internal call
  • Check response status and handle CalloutException in Apex
  • Apply visible loading states appropriately for slower, less predictable external calls

Prerequisites: "JSON Serialization and Callouts"

Callouts Can Fail in More Ways Than Internal Calls

An internal Apex or LDS operation (Module 6, Module 7) fails in fairly predictable ways. An external HTTP callout adds genuinely new failure modes: the request can time out, the external system can return a non-2xx status code, or the external system can simply be down or unreachable — none of which is under this org's control at all.

Handling Failures in Apex

public static WeatherSummary getCurrentWeather(String city) {
    try {
        HttpResponse response = new Http().send(buildRequest(city));

        if (response.getStatusCode() != 200) {
            throw new AuraHandledException('Weather service returned an unexpected response.');
        }

        WeatherResponse raw = (WeatherResponse) JSON.deserialize(response.getBody(), WeatherResponse.class);
        return new WeatherSummary(raw.temperature, raw.conditions);
    } catch (CalloutException e) {
        throw new AuraHandledException('Could not reach the weather service. Please try again.');
    }
}

Checking response.getStatusCode() explicitly — rather than assuming success — catches a non-2xx response. Wrapping the callout in try/catch for CalloutException catches network-level failures like timeouts. Both paths translate into a clear AuraHandledException message (Module 7), never a raw, leaked exception detail.

Loading States on the Client

Module 8's loading-state pattern (disable the button, show a spinner, reset in finally) applies here too — but external calls are often meaningfully slower and less predictable than an internal call, making a visible loading state even more important. A user staring at an unresponsive button during a slow external callout, with no loading indicator at all, is a genuinely poor experience this pattern directly prevents.

Exercise

Add a status code check to this callout that throws an AuraHandledException with the message "Inventory service error." if the response is not a 200.

Show hint

Check response.getStatusCode() after the callout.

APEX

Exercise

Challenge: explain, as a comment, why a visible loading state matters even more for an external callout than for a typical internal LDS operation.

Show hint

Think about the relative speed and predictability of each.

JAVASCRIPT

Loading States, Errors, and Timeouts in Integrations Quiz

1. What is a failure mode unique to external callouts, not typical of internal Apex/LDS calls?

2. What Apex exception type is caught to handle network-level callout failures like timeouts?

3. Why check response.getStatusCode() explicitly rather than assuming success?

4. Why does a visible loading state matter more for external callouts than typical internal calls?

5. What should a callout failure be translated into before reaching the component?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

An external callout can fail in ways an internal Apex/LDS call never does — timeouts, non-2xx responses, and systems outside the org's control entirely — so handling failure explicitly matters even more here.