Add source method for custom error type #5

Open
opened 2021-11-09 14:12:23 +00:00 by glyph · 1 comment
Owner

We need to add source() to provide a way for library users to access the underlying error (essentially, the stderr of the sbotcli which was invoked).

The Rust docs offer a clear example of how to implement this:

https://doc.rust-lang.org/std/error/trait.Error.html#method.source

We need to add `source()` to provide a way for library users to access the underlying error (essentially, the `stderr` of the `sbotcli` which was invoked). The Rust docs offer a clear example of how to implement this: https://doc.rust-lang.org/std/error/trait.Error.html#method.source
glyph added the
enhancement
label 2021-11-09 14:12:23 +00:00
Author
Owner

Thinking about this a bit more with an example:

Here's the output when I call sbotcli call whoami without a running go-sbot (it returns two errors):

level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\""
level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF"

In our library as it currently exists, this would result in the following error being displayed to the library user:

Error calling whoami: level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\""
level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF"

My current thought is that the basic error message that gets returned to the library caller should read like this:

Error calling whoami: failed to open unix path \"/home/glyph/.ssb-go/socket\"

or perhaps

Error calling whoami: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519

Then the err.source() would return the full stderr from the failed call:

level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\""
level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF"
Thinking about this a bit more with an example: Here's the output when I call `sbotcli call whoami` without a running `go-sbot` (it returns two errors): ``` level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\"" level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF" ``` In our library as it currently exists, this would result in the following error being displayed to the library user: ``` Error calling whoami: level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\"" level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF" ``` My current thought is that the basic error message that gets returned to the library caller should read like this: `Error calling whoami: failed to open unix path \"/home/glyph/.ssb-go/socket\"` or perhaps `Error calling whoami: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519` Then the `err.source()` would return the full `stderr` from the failed call: ``` level=warn client="unix-path based init failed" err="ssbClient: failed to open unix path \"/home/glyph/.ssb-go/socket\"" level=error run-failure="init: failed to connect to 127.0.0.1:8008|@ZPKL+7GG2D5rAwu74tr1CHZNr+ODxdCCA9zwFFIZXl4=.ed25519: error dialing: error wrapping connection: secrethandshake: failed during data transfer of receiving server auth: EOF" ```
Sign in to join this conversation.
No description provided.