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
Adding a better error message for download mode #1492
base: main
Are you sure you want to change the base?
Changes from 2 commits
0224e53
fa2631c
834eba4
f45a03b
4715103
e928a0f
32e7c28
a846557
25b5860
36b6d62
2c6b90b
5b85142
65662f5
e962a4d
1b29dcf
d6a7cbd
de71cb7
cc70e12
a5eda16
198d574
ee8f4d1
2cc9f84
9695c36
726b77c
4320101
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ import ( | |
"github.com/gomods/athens/pkg/download" | ||
"github.com/gomods/athens/pkg/download/addons" | ||
"github.com/gomods/athens/pkg/download/mode" | ||
"github.com/gomods/athens/pkg/errors" | ||
"github.com/gomods/athens/pkg/log" | ||
"github.com/gomods/athens/pkg/module" | ||
"github.com/gomods/athens/pkg/stash" | ||
|
@@ -98,7 +99,10 @@ func addProxyRoutes( | |
} | ||
st := stash.New(mf, s, stash.WithPool(c.GoGetWorkers), withSingleFlight) | ||
|
||
df, err := mode.NewFile(c.DownloadMode, c.DownloadURL) | ||
df, err := mode.NewFile(c.DownloadMode, c.DownloadURL, errors.ConfigError( | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. though this functional pattern is pretty awesome, it's a bit of an overkill just to add 2 lines of text to an error:
|
||
"DownloadMode", | ||
"https://docs.gomods.io/configuration/download/", | ||
)) | ||
if err != nil { | ||
return err | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package errors | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
// ConfigErrFn is a function that can create a new configuration error. You pass it a | ||
// message specific to the error you found when you were validating configuration, | ||
// and it knows how to print out the actual configuration name and other helpful information. | ||
type ConfigErrFn func(string) error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here since this is specifically a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @marwan-at-work I wanted to but can't because the config package imports mode which needs all the config error stuff. Do you think it should be somewhere besides there? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry, I meant the So that said, all of my suggestion are meant for keeping this functionality local to wherever the error is happening, in this case inside Thanks @arschles ✌️ |
||
|
||
type configErr struct { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we need a new error type? I think we can leverage the existing one. And instead of making a new error type, we can just make a new error Kind like |
||
str string | ||
} | ||
|
||
func (c configErr) Error() string { | ||
return c.str | ||
} | ||
|
||
// ConfigError returns a function that creates a configuration error to be printed | ||
// to the terminal. Call this function and pass its return value down the call stack to | ||
// functions that validate configuration fields. | ||
// | ||
// The function that ConfigError returns | ||
// | ||
// For example: | ||
// | ||
// downloadModeFn := ConfigError("DownloadMode (ATHENS_DOWNLOAD_MODE)") | ||
// err := doThingsWithDownloadMode(configStruct, downloadModeFn) | ||
func ConfigError(field string, url string) ConfigErrFn { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is pretty cool and functional :) but we can probably move this into the config package, because nobody else is leveraging it and so no need to increase the API surface of the errors package, we can just use the already exposed functionality. |
||
return func(helpText string) error { | ||
slc := []string{ | ||
fmt.Sprintf("There was a configuration error with %s. %s", field, helpText), | ||
} | ||
if url != "" { | ||
slc = append(slc, fmt.Sprintf("See %s for more information.", url)) | ||
} | ||
return &configErr{str: strings.Join(slc, "\n")} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd just |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,9 @@ package errors | |
func IsNotFoundErr(err error) bool { | ||
return Kind(err) == KindNotFound | ||
} | ||
|
||
// IsConfigErr returns true if the given err is a configuration error | ||
func IsConfigErr(err error) bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we probably don't need this if we just do But furthermore, I don't think we need a new config error kind, because we don't programmatically do anything about it anyway, we just log the error and exit. |
||
_, ok := err.(*configErr) | ||
return ok | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so all that we're doing here is choosing not to wrap the error with "error adding proxy routes (message)". I think, instead of doing that, we can just pick a better wrapper-message and then we don't need this exposed API at all, we can just say:
"error initializing Athens: %s"
below.