Catch/recover from flux http error

Hello.

I can not find any info about handling errors from http.post/http.get (DNS lookup errors, https cert errors, timeouts, etc.). How can I have that connection error as a string (or at least a null response) and continues script execution?

Or maybe there is a general way of error handling? Like try { 0/0 } catch() { ... }

Hello @3bl3gamer,
Welcome!
You’ll have to use array.from() like:

import "array"
import "experimental/http"

got = http.get(
  url: "myurl",
  headers: { Authorization:"Token tweiriuosfjdlk=="},
  timeout: 30s
)

array.from(rows: [{_time: 2020-01-01T00:00:00Z, _value: got.statusCode}])
|> yield()

But these issues are related to what you’re asking about, I encourage you to take a look/comment.

Hello!
Not sure how array.from() will help here. For example, I have currently no service at https://127.0.0.1:81, and if I run:

import "array"
import "experimental/http"

got = http.get(
  url: "https://127.0.0.1:81",
  timeout: 30s
)

array.from(rows: [{_time: 2020-01-01T00:00:00Z, _value: got.statusCode}])
  |> yield()

…script still just fails with error calling function "get" ... connect: connection refused (no tables in output).

Ah if you have no service then that won’t work. But I’m confused why are you trying to use http.get() against an unavailable service? Can you please provide some more context around what you’re trying to do so I can help you better?

I want to make a task which checks my service availability and immediately sends notification unless it’s not. I assume that notification service is always available, but my service may be down. Currently this does not work, because if my service is down, the whole script will fail and there will be no notification.