Tutorial: Handle error messages with the Error Rail
Note: This is a self-contained tutorial and not part of the previous set.
In this tutorial, we will be showing you how to use the error rail with
HttpClient::get to display clean error messages. We will be using the
PokeAPI, as it is publicly available and requires no
special authentication.
- Create a new function from the omnibox or sidebar. Call this function
pokeSearchand add a Pokemon parameter with typeString.

- Now, use your new function by creating a new REPL named search and calling
your
pokeSearchfunction with"ditto"as the parameter.

- Return to your function and check your traces - you should see a new one with
"ditto".

- Create your URL by concatenating
https://pokeapi.co/api/v2/pokemon/with your parameter.

- Write your
HttpClient::getcall.

- Return the results of your
HttpClient::get. Note the check mark in the circle to the right of your call - this is indicating that your function is on the Error Rail, and that it isn't returning an error.

- Return to your
searchREPL and enter anything other than the name of an actual Pokemon. I chose"pikawho". Then, run your REPL again to see theErrortrace.

- Return to your
pokeSearchfunction, and note that the check mark has changed to a red exclamation point, indicating that there is an error.

- Place your cursor in
HttpClient::get,open the command palette, and selecttake-function-off-rail.

- Once you do, you'll notice that the Error Rail icon on the right disappears, and your return trace will update.

- Now, we're going to use a
matchondatato return the result we want. Here, I've said that ifdatareturns anOkwe can just return data, and ifdatareturns anErrorwe will print out a nice error message.

- Return to your
searchREPL, and re-run it. You'll see your nicely formatted error message.

- Try again with the name of a real Pokemon to confirm that, when there is no
error,
datais returned.

Congratulations! You've now successfully taken a function off of the error rail to write a custom error message!