# Templater Error Handling

**URL:** <https://forum.obsidian.md/t/templater-error-handling/53627>\
**Category:** Help\
**Tags:** templater\
**Created:** [February 2, 2023, 2:41pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627 "2023-02-02T14:41:08Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 2, 2023, 2:41pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/1 "2023-02-02T14:41:08Z")

</div>

I have a Templater template for my Daily Notes that includes `<% tp.user.weathernow() %>` which is a user function that runs `curl "wttr.in/Mequon?format=2"`. It has been working great for a couple weeks, but today wttr.in appears to be down, and the consequence is that Templater errors out, and I can’t create a daily note (note opens as blank with no content from the template).

Is there a way to make adding the weather (or anything else for that matter) fail more gracefully, like an if/then that says if error, just put “Weather Not Available”.

---

<div class="post-metadata">

**Author:** ![mr\_abomination](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mr_abomination/32/27118_2.png) [@mr\_abomination](https://forum.obsidian.md/u/mr_abomination)\
**Post date:** [February 2, 2023, 9:52pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/2 "2023-02-02T21:52:14Z")

</div>

What is the code for the entire user function that grabs the weather?

You can also open the developer console to get a better idea what’s causing the problem by pressing `Ctrl`+`Shift`+`i` or `Cmd`+`Shift`+`i`

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 3:44am UTC](https://forum.obsidian.md/t/templater-error-handling/53627/3 "2023-02-03T03:44:14Z")

</div>

The curl command I listed is all it runs, it returns something like “☀🌡+21°F 🌬↗13mph”. The site is back up, but I’m still wondering if Templater has any error handling capabilities in case this type of thing happens in the future. I did open the console, and I don’t recall the exact message, but it wasn’t much more useful than “the weather thing isn’t working”.

---

<div class="post-metadata">

**Author:** ![geoffb](https://forum.obsidian.md/user_avatar/forum.obsidian.md/geoffb/32/42382_2.png) [@geoffb](https://forum.obsidian.md/u/geoffb)\
**Post date:** [February 3, 2023, 6:32am UTC](https://forum.obsidian.md/t/templater-error-handling/53627/4 "2023-02-03T06:32:47Z")

</div>

If you use a [JavaScript execution command](https://silentvoid13.github.io/Templater/commands/execution-command.html#javascript-execution-command) (`<%* ... %>` instead of `<% ... %>`), you can wrap your function in a try/catch, which will let you handle things gracefully the function if it errors out. Could you try something like this?

```auto
<%*
try {
  tp.user.weathernow()
} catch (e) {
  console.log(e)
}
%>

```

This way, things should behave as normal when they work. If the function fails, you should instead get the error logged to the console, but there will be no return and the rest of the template should work fine.

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 3:40pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/5 "2023-02-03T15:40:39Z")

</div>

This looks like what I was expecting to be possible, but I tried the code and it doesn’t return anything, even when `<% tp.user.weathernow() %>` by itself works. Is there another plugin I need beyond Templater to use JavaScript execution commands?

---

<div class="post-metadata">

**Author:** ![geoffb](https://forum.obsidian.md/user_avatar/forum.obsidian.md/geoffb/32/42382_2.png) [@geoffb](https://forum.obsidian.md/u/geoffb)\
**Post date:** [February 3, 2023, 3:52pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/6 "2023-02-03T15:52:17Z")

</div>

I didn’t test the function itself since I don’t have it implemented—only that the try/catch block works in Obsidian. I wonder if perhaps `Settings -> Templater -> Enable User System Command Functions` needs to be toggled on if it’s not already (though if it weren’t, I’d expect the function wouldn’t have run before)?

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 10:03pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/7 "2023-02-03T22:03:01Z")

</div>

I do have that setting enabled. I hacked around a bit and finally got something to work:

```auto
<%* try { %><% tp.user.weathernow() %><%* } catch (e) { %>"Could not retrieve weather."<%* } %>

```

Seemed like the Javascript part wasn’t able to output, so I wrapped it around what I already had working and that seemed to do the trick. Thanks for pointing me in the right direction!

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 3, 2023, 10:19pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/8 "2023-02-03T22:19:36Z")

</div>

When you switch to dynamic execution block, `<%*`, the output system changes, you need to append output to the `tR` variable.

You made it work since you went out of that block, and into a normal template block.

You could also have done:

```auto
<%*
try {
  tR = tp.user.weathernow()
} catch (e) {
  console.log(e)
}
%>

```

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 10:28pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/9 "2023-02-03T22:28:49Z")

</div>

I just tried that, but instead of getting the weather, everything in the template up to the end of that block was replaced with `[object Promise]` in the note.

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 3, 2023, 10:32pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/10 "2023-02-03T22:32:37Z")

</div>

Ok, my bad, you do need to await it, since it uses asynchronous functions.

So you’ll need something like `tR = await tp.user.weathernow()`.

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 10:39pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/11 "2023-02-03T22:39:37Z")

</div>

I added `await` to your suggested code bock and it seems to work, but everything before the code block in the template doesn’t show up in the note (everything after it does show up).

---

<div class="post-metadata">

**Author:** ![holroy](https://forum.obsidian.md/user_avatar/forum.obsidian.md/holroy/32/77699_2.png) [@holroy](https://forum.obsidian.md/u/holroy)\
**Post date:** [February 3, 2023, 10:43pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/12 "2023-02-03T22:43:39Z")

</div>

You would need to add any output to the `tR` variable in the entire dynamic execution code block. It’s part of the trade of to allow for arbitrary javascript to be run.

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 3, 2023, 10:54pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/13 "2023-02-03T22:54:51Z")

</div>

Gotcha, I’m very new to this environment. For now I’ll keep what I have working, but good to know for down the road when I eventually need to do something like that. Thanks!

---

<div class="post-metadata">

**Author:** ![mr\_abomination](https://forum.obsidian.md/user_avatar/forum.obsidian.md/mr_abomination/32/27118_2.png) [@mr\_abomination](https://forum.obsidian.md/u/mr_abomination)\
**Post date:** [February 4, 2023, 4:27pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/14 "2023-02-04T16:27:56Z")

</div>

While I encourage you to experiment on your own and see what works, this is what has worked for me in the past.

Have an execution block at the very top of the note that does all the complicated calculations, querying, or inputs, the save all results in variables. Then, lower down, just output any of the variables you need.

Example:

```javascript
---
tag: daily
---
<%*
let currentWeather

try {
  currentWeather = await tp.user.weathernow()
} catch (e) {
  console.log(e)
  currentWeather = "Cannot fetch Weather"
}

[Any other JS stuff]
_%>

[Rest of the template]

The weather is: <% currentWeather %>

```

You can make use of [Templater’s Whitespace Control](https://silentvoid13.github.io/Templater/commands/whitespace-control.html) to help hide the Execution block.

Hope this helps!

---

<div class="post-metadata">

**Author:** ![jpfieber](https://forum.obsidian.md/user_avatar/forum.obsidian.md/jpfieber/32/41532_2.png) [@jpfieber](https://forum.obsidian.md/u/jpfieber)\
**Post date:** [February 4, 2023, 7:39pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/15 "2023-02-04T19:39:41Z")

</div>

Works like a charm. I really like this approach, makes sense for multiple reason. Thanks for sharing!

---

<div class="post-metadata">

**Author:** ![system](https://forum.obsidian.md/uploads/default/original/3X/b/e/bec35fbd23ab28909bb6bfe62cb32eb82dd34019.png) [@system](https://forum.obsidian.md/u/system)\
**Post date:** [February 11, 2023, 7:40pm UTC](https://forum.obsidian.md/t/templater-error-handling/53627/16 "2023-02-11T19:40:05Z")

</div>

Closed on February 11, 2023, 7:40pm UTC

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
