Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement api to accept empty error #94

Open
Emyrk opened this issue Jan 22, 2021 · 4 comments
Open

Implement api to accept empty error #94

Emyrk opened this issue Jan 22, 2021 · 4 comments

Comments

@Emyrk
Copy link
Member

Emyrk commented Jan 22, 2021

Zerolog has this nifty api log.Err(err error) that is Error level if err != nil and Info level if err == nil

The basic gist to avoid this common code:

authenticated, err := auth()
if err != nil {
  slog.Error(ctx, "failed to authorize user"
    slog.Error(err),
    slog.F("user", "alice")
  )
  return
}

slog.Info(ctx, "authorized user",
    slog.F("user", "alice"),
)
...

and you can just do (the message becomes the action, and the Error level indicates the action failed):

authenticated, err := auth()
// Unsure exactly on the API, still adapting to slog, so unsure what would be "natural" 
slog.Err(err).(ctx, "authorize user", 
  slog.F("user", "alice")
)

if err != nil {
  return
}
...

It is a small feature I miss. Let me know what you think.

@nhooyr
Copy link
Contributor

nhooyr commented Jan 25, 2021

This is interesting but it seems very similar to using throw/catch versus Go's indented error paths idiom.

@Emyrk
Copy link
Member Author

Emyrk commented Jan 25, 2021

This is interesting but it seems very similar to using throw/catch versus Go's indented error paths idiom.

I can see what you mean, but it does not affect how to logically handle the error, just how to log it.

@nhooyr
Copy link
Contributor

nhooyr commented Jan 25, 2021

I can see what you mean, but it does not affect how to logically handle the error, just how to log it.

Let me just say first that I fully agree Go takes the whole indent every error path thing way too far and it gets very repetitive. But slog aims to follow Go idioms. And handling an error is no different than logging it.

I wouldn't stop such a change though if the Go team @cdr was in majority support.

@nhooyr
Copy link
Contributor

nhooyr commented Jan 25, 2021

Or sorry not no different, I meant logging is part of handling an error.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants