Level 1 · Lesson 5
Lay out the car details card
Level 1 · Lesson 5Goal of this lesson
Make the resolver report problems instead of failing silently, then give the card a waiting state, an empty state, an error state and the full specs.
Behind, or starting here? Download the code as it should be before this lesson, or in the course workspace run course-catch-up level-1/after-04-look-up-the-car in the folder above your app. It offers to bring your own app folder to this point and keeps its app id (the id: line in manifest.yml, which links the code to your app on the site), so it stays the same app. Your old files are kept in a copy next to it.
Right now the card has one shape: the car’s details, or Loading… forever if anything went wrong. A real card has four: waiting, problem, nothing found, and the car’s details. The resolver changes first, so the card has something to show for each.
Use the tunnel for this lesson if you like: none of these steps touch the manifest. In a codespace in your browser, deploy after each change instead, as in lesson 2. Start it with the key exported, as in lesson 3.
Step 1: Let the resolver report problems
Locked until the step before it is done.
When findCar throws an error (stops and reports a problem), the resolver call fails and the card never hears why. Catch the error, write it to the log for you, and send a short message back as data for the person looking at the card.
try and catch are how code says “attempt this, and if it goes wrong, do that instead”. Everything inside try runs as usual. If anything in it throws an error, the code jumps straight to catch, which receives the error and decides what happens next. It works like an automation rule with an if/else branch: the normal path, and a fallback path for when something fails.
src/resolvers/index.jsCatch lookup errors and send them back as data.
resolver.define('getCar', async () => { | |||
const make = 'BMW'; | |||
const model = '3 Series'; | |||
Removed: const car = await findCar(make, model, process.env.API_NINJAS_KEY); | |||
Removed: return { query: `${make} ${model}`, car }; | |||
Added: const query = `${make} ${model}`; | |||
Added: try { | |||
Attempt what follows. If anything in it throws an error, jump to | |||
Added: const car = await findCar(make, model, process.env.API_NINJAS_KEY); | |||
Added: return { query, car }; | |||
Added: } catch (error) { | |||
Something went wrong: | |||
Added: console.error(`Car lookup failed for "${query}":`, error); | |||
Write the full problem to the app’s logs, for you. | |||
Added: return { query, car: null, error: error.message }; | |||
Send the card no car, plus the short sentence from the error, for the person looking at it. | |||
Added: } | |||
}); | |||
| |||
export const handler = resolver.getDefinitions(); | |||
Why: Resolver
console.error writes a line to the app’s logs, a running record that only you, the developer, can read; lesson 7 shows how. It is the app’s equivalent of an automation rule’s audit log. error.message is one of the plain sentences findCar throws, such as the one about a missing key, so it is safe to show to the person looking at the card.
Stuck?One common problem, and its fix
I changed code in src/
Open in the FAQWith
forge tunnelrunning on your own computer, save the file and reload the Jira page: the tunnel serves your laptop’s code. Without the tunnel, or in a codespace in your browser, deploy:RunTerminal forge deployThe site picks up the new version on its own. You do not need to install again.
Step 2: Import more UI Kit components
Locked until the step before it is done.
Each state needs a UI Kit component:
Spinnerwhile waiting.SectionMessagefor problems and empty results.ListandListItemfor what the car comes in.
src/frontend/index.jsxAdd the list, spinner and message components.
import React, { useEffect, useState } from 'react'; | |||
Removed: import ForgeReconciler, { Heading, Inline, Lozenge, Stack, Text } from '@forge/react'; | |||
Added: import ForgeReconciler, { Heading, Inline, List, ListItem, Lozenge, SectionMessage, Spinner, Stack, Text } from '@forge/react'; | |||
The same list of borrowed UI Kit pieces, with four more: | |||
import { invoke } from '@forge/bridge'; | |||
| |||
const App = () => { | |||
Why: UI Kit
Step 3: Handle waiting, problems and nothing found
Locked until the step before it is done.
Return early for each state before the full card. “Return early” means the card checks each situation in turn, and as soon as one matches, it draws that and stops reading. It is like a rule with several conditions checked from the top: the first one that fits wins. So the order matters: no answer yet, then a problem, then an empty result.
src/frontend/index.jsxShow a spinner, a problem or an empty card before the full card.
useEffect(() => { | |||
invoke('getCar').then(setResult); | |||
}, []); | |||
Removed: const car = result?.car; | |||
Added: if (!result) return <Spinner label="Asking the car data service" />; | |||
No answer yet: show a spinning wheel and stop here. | |||
Added: if (result.error) { | |||
The resolver reported a problem: show it in a red message box and stop here. | |||
Added: return ( | |||
Added: <SectionMessage appearance="error" title="The workshop could not look up the car"> | |||
Added: <Text>{result.error}</Text> | |||
Added: </SectionMessage> | |||
Added: ); | |||
Added: } | |||
Added: if (!result.car) { | |||
An answer, but no car: show a blue message that says which car was asked about, and stop here. | |||
Added: return ( | |||
Added: <SectionMessage appearance="information" title={`No car data for “${result.query}”`}> | |||
Added: <Text>Start the work item summary with a make and model, such as BMW 3 Series, then reload.</Text> | |||
Added: </SectionMessage> | |||
Added: ); | |||
Added: } | |||
Added: const { car } = result; | |||
Only now does the card go on to the full layout, and by now there is always a car. This takes it out of the answer. | |||
return ( | |||
<Stack space="space.200"> | |||
<Inline space="space.100" alignBlock="center"> | |||
Why: UI Kit
After these checks, car always exists, so the full card below no longer needs car ? everywhere. Remember that “nothing found” is not an error from the service: it answered, with every list empty, and toCar turned that into null.
Stuck?One common problem, and its fix
The card says there is no car data for my work item
Open in the FAQThe card reads the make and model from the start of the summary, before the first dash with a space on each side. Start the summary with a car the service knows, such as “Volkswagen Golf – oil service”, and reload. Makes of two words, such as Land Rover, work only if the helper in
src/cars/summary.jslists them.
Step 4: Lay out the full car details card
Locked until the step before it is done.
Now lay out the whole card: the make and model with the first year as a tag, then what the model comes in as a list. A facet with no values is left out.
src/frontend/index.jsxChange 1 of 3Make and model with the first year, then what the model comes in as a list.
); | |||
} | |||
const { car } = result; | |||
Added: const facts = [ | |||
A list of pairs: a label, and the values for it. | |||
Added: ['Fuel', car.fuel], | |||
Added: ['Transmission', car.transmission], | |||
Added: ['Drive', car.drive], | |||
Added: ['Cylinders', car.cylinders], | |||
Added: ['Versions', car.versions], | |||
Added: ].filter(([, values]) => values.length > 0); | |||
Keep only the pairs that have at least one value. | |||
return ( | |||
<Stack space="space.200"> | |||
<Inline space="space.100" alignBlock="center"> | |||
src/frontend/index.jsxChange 2 of 3 return ( | |||
<Stack space="space.200"> | |||
<Inline space="space.100" alignBlock="center"> | |||
Removed: <Heading size="medium">{car ? `${car.make} ${car.model}` : 'Loading...'}</Heading> | |||
Removed: {car && <Lozenge>{car.fuel.join(' · ')}</Lozenge>} | |||
Added: <Heading size="medium">{`${car.make} ${car.model}`}</Heading> | |||
Added: {car.since && <Lozenge>{`Since ${car.since}`}</Lozenge>} | |||
A Since tag, only when the service gave a year. | |||
</Inline> | |||
src/frontend/index.jsxChange 3 of 3 </Inline> | |||
Removed: <Text>{car ? `Since ${car.since}` : ''}</Text> | |||
Added: <Heading size="small">Comes in</Heading> | |||
Added: <List type="unordered"> | |||
Added: {facts.map(([name, values]) => ( | |||
For each pair left in | |||
Added: <ListItem key={name}>{`${name}: ${values.join(', ')}`}</ListItem> | |||
One line, such as “Fuel: Petrol, Diesel”. | |||
Added: ))} | |||
Added: </List> | |||
</Stack> | |||
); | |||
}; | |||
Why: UI Kit
facts is a short list of pairs, such as “Fuel” with Petrol, Diesel and Hybrid. .filter keeps only the pairs that have at least one value, so a facet with nothing in it does not show an empty line. .map then turns each remaining pair into one line of the list.
React needs a key on each list item: a name that tells the items apart, so React knows which line is which when it redraws the card. The facet’s name works here, because each facet appears only once.
Step 5: Try it, then break it on purpose
Locked until the step before it is done.
Deploy if the tunnel is not running, then reload the BMW 3 Series repair task:
forge deployCar details
BMW 3 Series Since 1987
Comes in
- Fuel: Petrol, Diesel, Hybrid
- Transmission: Automatic, Manual, Dual-clutch
- Drive: Rear-wheel drive, All-wheel drive, Four-wheel drive
- Cylinders: 4, 6, 3
- Versions: 320d, 320i, 2.0, 330i, 330d
The versions are the service’s badges: trim codes such as 320d mixed with engine sizes such as 2.0. That is the data as it comes; the card does not try to tidy it.
Then see a state you wrote:
- Change
'BMW'to'Moon'and'3 Series'to'Buggy'insrc/resolvers/index.jsand save. - With the tunnel running, just reload; without it, run
forge deployfirst, then reload. The card shows the information message instead of failing. - Change them back when you have seen it, and save (and deploy again if you are not using the tunnel).
Stuck?2 common problems, and their fixes
I changed code in src/
Open in the FAQWith
forge tunnelrunning on your own computer, save the file and reload the Jira page: the tunnel serves your laptop’s code. Without the tunnel, or in a codespace in your browser, deploy:RunTerminal forge deployThe site picks up the new version on its own. You do not need to install again.
The card says there is no car data for my work item
Open in the FAQThe card reads the make and model from the start of the summary, before the first dash with a space on each side. Start the summary with a car the service knows, such as “Volkswagen Golf – oil service”, and reload. Makes of two words, such as Land Rover, work only if the helper in
src/cars/summary.jslists them.
Compare with yours: download the code after this lesson.