Skip to main content

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.

  1. Create a new function from the omnibox or sidebar. Call this function pokeSearch and add a Pokemon parameter with type String.

error-rail/pokeSearch.png

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

error-rail/ditto.png

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

error-rail/dittoTrace.png

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

error-rail/url.png

  1. Write your HttpClient::get call.

error-rail/data.png

  1. 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.

error-rail/returnData.png

  1. Return to your search REPL and enter anything other than the name of an actual Pokemon. I chose "pikawho". Then, run your REPL again to see the Error trace.

error-rail/error.png

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

error-rail/errorpoint.png

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

error-rail/offrail.png

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

error-rail/errorTrace.png

  1. Now, we're going to use a match on data to return the result we want. Here, I've said that if data returns an Ok we can just return data, and if data returns an Error we will print out a nice error message.

error-rail/niceError.png

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

error-rail/replError.png

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

error-rail/correctName.png

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