| 1 |
--- |
| 2 |
title: try |
| 3 |
description: Returns a TryValue object after evaluating the given expression. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: TryValue |
| 10 |
signatures: ['try EXPRESSION'] |
| 11 |
--- |
| 12 |
|
| 13 |
{{< new-in 0.141.0 />}} |
| 14 |
|
| 15 |
The `try` statement is a non-standard extension to Go's [`text/template`][] package. It introduces a mechanism for handling errors within templates, mimicking the `try-catch` constructs found in other programming languages. |
| 16 |
|
| 17 |
## Methods |
| 18 |
|
| 19 |
Use these methods on the `TryValue` object. |
| 20 |
|
| 21 |
`Err` |
| 22 |
: (`string`) Returns a string representation of the error thrown by the expression, if an error occurred, or returns `nil` if the expression evaluated without errors. |
| 23 |
|
| 24 |
`Value` |
| 25 |
: (`any`) Returns the result of the expression if the evaluation was successful, or returns `nil` if an error occurred while evaluating the expression. |
| 26 |
|
| 27 |
## Explanation |
| 28 |
|
| 29 |
By way of example, let's divide a number by zero: |
| 30 |
|
| 31 |
```go-html-template |
| 32 |
{{ $x := 1 }} |
| 33 |
{{ $y := 0 }} |
| 34 |
{{ $result := div $x $y }} |
| 35 |
{{ printf "%v divided by %v equals %v" $x $y .Value }} |
| 36 |
``` |
| 37 |
|
| 38 |
As expected, the example above throws an error and fails the build: |
| 39 |
|
| 40 |
```terminfo |
| 41 |
Error: error calling div: can't divide the value by 0 |
| 42 |
``` |
| 43 |
|
| 44 |
Instead of failing the build, we can catch the error and emit a warning: |
| 45 |
|
| 46 |
```go-html-template |
| 47 |
{{ $x := 1 }} |
| 48 |
{{ $y := 0 }} |
| 49 |
{{ with try (div $x $y) }} |
| 50 |
{{ with .Err }} |
| 51 |
{{ warnf "%s" . }} |
| 52 |
{{ else }} |
| 53 |
{{ printf "%v divided by %v equals %v" $x $y .Value }} |
| 54 |
{{ end }} |
| 55 |
{{ end }} |
| 56 |
``` |
| 57 |
|
| 58 |
The error thrown by the expression is logged to the console as a warning: |
| 59 |
|
| 60 |
```terminfo |
| 61 |
WARN error calling div: can't divide the value by 0 |
| 62 |
``` |
| 63 |
|
| 64 |
Now let's change the arguments to avoid dividing by zero: |
| 65 |
|
| 66 |
```go-html-template |
| 67 |
{{ $x := 42 }} |
| 68 |
{{ $y := 6 }} |
| 69 |
{{ with try (div $x $y) }} |
| 70 |
{{ with .Err }} |
| 71 |
{{ warnf "%s" . }} |
| 72 |
{{ else }} |
| 73 |
{{ printf "%v divided by %v equals %v" $x $y .Value }} |
| 74 |
{{ end }} |
| 75 |
{{ end }} |
| 76 |
``` |
| 77 |
|
| 78 |
Hugo renders the above to: |
| 79 |
|
| 80 |
```html |
| 81 |
42 divided by 6 equals 7 |
| 82 |
``` |
| 83 |
|
| 84 |
## Example |
| 85 |
|
| 86 |
Error handling is essential when using the [`resources.GetRemote`][] function to capture remote resources such as data or images. When calling this function, if the HTTP request fails, Hugo will fail the build. |
| 87 |
|
| 88 |
Instead of failing the build, we can catch the error and emit a warning: |
| 89 |
|
| 90 |
```go-html-template |
| 91 |
{{ $url := "https://broken-example.org/images/a.jpg" }} |
| 92 |
{{ with try (resources.GetRemote $url) }} |
| 93 |
{{ with .Err }} |
| 94 |
{{ warnf "%s" . }} |
| 95 |
{{ else with .Value }} |
| 96 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" alt=""> |
| 97 |
{{ else }} |
| 98 |
{{ warnf "Unable to get remote resource %q" $url }} |
| 99 |
{{ end }} |
| 100 |
{{ end }} |
| 101 |
``` |
| 102 |
|
| 103 |
In the above, note that the [context](g) within the last conditional block is the `TryValue` object returned by the `try` statement. At this point neither the `Err` nor `Value` methods returned anything, so the current context is not useful. Use the `$` to access the [template context][] if needed. |
| 104 |
|
| 105 |
> [!NOTE] |
| 106 |
> Hugo does not classify an HTTP response with status code 404 as an error. In this case `resources.GetRemote` returns `nil`. |
| 107 |
|
| 108 |
[`resources.GetRemote`]: /functions/resources/getremote/ |
| 109 |
[`text/template`]: https://pkg.go.dev/text/template |
| 110 |
[template context]: /templates/introduction/#template-context |