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

Add a method to set a public-facing message #22

Merged
merged 2 commits into from
Aug 20, 2024

Conversation

knpwrs
Copy link
Contributor

@knpwrs knpwrs commented Aug 20, 2024

The idea here is to provide a consistent key for error messages that are guaranteed to be safe to show to an end user. My use case is a global error handler for a web application that can result in error messages being shown on the screen, but I don't want to show just any message for any error, I want to only show strings that I know are deemed appropriate for end users to see.

@samber
Copy link
Owner

samber commented Aug 20, 2024

OMG, you're a genius.

I've seen such a thing in labstack/echo, but since I use gin-gonic/gin, I always coded workarounds. Embedding it in oops is much better!

In terms of API, I wonder if we should add a getter such as:

func GetPublic(err error, defaultPublicMessage string) string {
    var oopsError oops.OopsError
    if errors.As(err, &oopsError) {
        msg := oopsError.Public()
        if len(msg) > 0 {
            return msg
        }
        return defaultPublicMessage
    }
}

@samber
Copy link
Owner

samber commented Aug 20, 2024

The idea here is to provide a consistent key

BTW, the error builder brings a ".Code(string)" method if your error is a key.

@knpwrs
Copy link
Contributor Author

knpwrs commented Aug 20, 2024

In terms of API, I wonder if we should add a getter such as:

func GetPublic(err error, defaultPublicMessage string) string {
    var oopsError oops.OopsError
    if errors.As(err, &oopsError) {
        msg := oopsError.Public()
        if len(msg) > 0 {
            return msg
        }
        return defaultPublicMessage
    }
}

I like that, I'll push it up.

@knpwrs
Copy link
Contributor Author

knpwrs commented Aug 20, 2024

BTW, the error builder brings a ".Code(string)" method if your error is a key.

I saw that, but I'm not enterprise enough to define error codes yet 😆

@samber
Copy link
Owner

samber commented Aug 20, 2024

"work in progress" 🫣

@samber samber merged commit 7e53e1a into samber:main Aug 20, 2024
4 of 5 checks passed
@samber
Copy link
Owner

samber commented Aug 20, 2024

@knpwrs sorry for the late comment: I wonder if we should call this method .Human(...) instead ?

@knpwrs
Copy link
Contributor Author

knpwrs commented Aug 20, 2024

.Human is probably good as well. Though I can see .Public being used more generically, like for public-facing APIs where the errors are given user-facing strings for masking purposes.

@samber
Copy link
Owner

samber commented Aug 20, 2024

ok, lets keep public then

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

Successfully merging this pull request may close these issues.

2 participants