
An invalid latitude causes the weather API in this example to return an error instead of temperature data. Capturing that response makes its error details available to a Brickworks response transformer.

In this use case, you will extend the [weather example](/use-cases/brickworks-prepare-current-temperature-data) to return a temperature for valid coordinates and an error message for invalid coordinates. A content consumer can use the resulting error flag to distinguish these two outcomes.


<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

This option handles an external source that responds with an error. It does not handle connection failures such as timeouts or DNS errors, and this article does not describe recovery from those failures.

</div></div></div>


## Prerequisites
---

- Complete [Prepare current temperature data for content with Brickworks](/use-cases/brickworks-prepare-current-temperature-data), including the external request and context configuration.
- Keep the existing Open Meteo Connection external source available for reuse.
- Have [permissions](/docs/settings/identity-access-management/permissions/data-management-permissions#work-with-brickworks) to create an additional schema and configure its External Data field and response transformer.

## Process
---

In this use case, you will go through the following steps:
1. [Create a separate schema for error handling](#create-a-separate-schema-for-error-handling) that reuses the weather external source.
2. [Enable error details on its External Data field](#enable-error-details-on-the-external-data-field).
3. [Create the error-handling transformer](#create-the-error-handling-transformer) with successful-response and error-response branches.
4. [Preview successful and failed responses](#preview-successful-and-failed-responses) before activating the transformer.

## Create a separate schema for error handling
---

Use a separate schema for this example so that its output can be tested independently from the original schema. Reuse the external source without changing its request configuration.

1. Go to **Data Modeling Hub > Schemas > New schema**.
2. Set **Display name** to `Current Temperature (Error Handling)`
3. Set the schema type to **Singleton**.
4. Add an **External Data** field with the display name `Open Meteo Connection`
5. Set the field's API name to `openMeteoConnection`, which the transformer below references.
6. Select the existing `Open Meteo Connection` external source.

## Enable error details on the External Data field
---

1. Open the configuration of `openMeteoConnection` in the new schema.
2. Enable **Return error details when the source responds with an error**.
3. Save the schema. 

A successful call returns the weather payload with `current` and `current_units`, without a `response.status` key. The transformer below uses this difference to select a branch.

<figure>
<img src="/api/docs/image/b19c7d20317c68447af98693af18068fd4592c91/use-cases/all-cases/_gfx/enable_error_details.png" alt="External source field configuration"  class="medium">
<figcaption>External source field configuration</figcaption>
</figure>

## Create the error-handling transformer
---
For the invalid latitude example, the field returns an object that contains an HTTP status and the provider's error body. The relevant excerpt is:

```json
{
  "response.status": 400,
  "response.body": {
    "reason": "Latitude must be in range of -90 to 90°. Given: 200.0.",
    "error": true
  }
}
```

The keys `response.status` and `response.body` contain literal dots. The transformer accesses them with bracket notation, such as `weather["response.status"]`

The transformer returns `error: false` with the temperature after a successful request. For the documented error response, it returns `error: true`, a placeholder temperature, and an error message.

1. In the schema editor, open **Response transformers**.
2. Select **Edit > Add transformer**.
3. Name the transformer `Error Handling`
4. Enter the following script:

        
   <pre><code class="language-javascript">{
     var weather = generated.openMeteoConnection;

     if (weather["response.status"]) {
       var body = weather["response.body"] || {};
       return {
         temperature: "N/A",
         error: true,
         errorMessage: body.reason || "Unknown error"
       };
     }

     return {
       temperature: weather.current.temperature_2m + weather.current_units.temperature_2m,
       error: false
     };
   }</code></pre>


5. Click **Save** or **Save and close**.

<figure>
<img src="/api/docs/image/b19c7d20317c68447af98693af18068fd4592c91/use-cases/all-cases/_gfx/error_handling_transformer.png" alt="Error handling transformer script"  class="medium">
<figcaption>Error handling transformer script</figcaption>
</figure>


<div class="admonition admonition-important"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M12 8v4m0 4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

The script is specific to the response shapes above. It does not handle every possible missing or malformed payload. Its `Unknown error` fallback covers a missing or empty `reason` in the selected error branch; it is not a fallback for timeouts or failed record generation.

</div></div></div>


## Preview successful and failed responses
---

1. Open the record preview: 
    1. Set the context.
    2. Select `Error Handling` on the **Transforming response** tab.


<div class="admonition admonition-note"><div class="admonition-icon"><svg xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2.5"><path stroke-linecap="round" stroke-linejoin="round" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z" /></svg></div><div class="admonition-body"><div class="admonition-content">

Use the inactive transformer for these checks before you change the active configuration.

</div></div></div>




### Check valid coordinates
---

1. Use the [record preview](/docs/assets/brickworks/quick-start/creating-a-record#previewing-records) to check the results. 
    1. Set the context in **Additional parameters** to `lat` to `50` and `lng` to `20`.
    2. On the **Transforming response** tab, select the `Error Handling` transformer.
2. Generate the preview.
3. Check that `error` is `false` and that `temperature` combines the value and unit returned by the weather provider.

For the sample weather response, the preview returns:

```json
{
  "temperature": "23.6°C",
  "error": false
}
```

The temperature can differ in a new request.

### Check invalid coordinatesf
---

1.  Use the [record preview](/docs/assets/brickworks/quick-start/creating-a-record#previewing-records) to check the results. 
    1. Set the context in **Additional parameters** to `lat` to `200` and keep `lng` at `20`
    2. On the **Transforming response** tab, select the `Error Handling` transformer.
2. Generate the preview.
3. Check that `error` is `true`, `temperature` is `N/A`, and `errorMessage` contains the provider's reason.

For the sample error response, the preview returns:

```json
{
  "temperature": "N/A",
  "error": true,
  "errorMessage": "Latitude must be in range of -90 to 90°. Given: 200.0."
}
```

Once the preview results are confirmed, select `Error Handling` as the active transformer for this schema, click **Apply**, and save the schema when ready to use the output.

<figure>
<img src="/api/docs/image/b19c7d20317c68447af98693af18068fd4592c91/use-cases/all-cases/_gfx/response_preview.png" alt="The view of the 'Preview record' tab"  class="full">
<figcaption>Preview record</figcaption>
</figure>

## What's next
---

For another external source, define which provider responses represent each business outcome, then map them to the fields expected by the consumer. Adapt and test the condition and error-body handling for that source instead of reusing the weather-specific script unchanged.

## Read more
---
- [Brickworks](/docs/assets/brickworks)
- [Response transformers](/docs/assets/brickworks/response-transformers)
