Refactor http error handling

This commit is contained in:
bagtoad 2024-10-09 22:26:03 -06:00
parent 8abed17a00
commit 9b94bee895
2 changed files with 10 additions and 4 deletions

View file

@ -81,8 +81,11 @@ func viewRun(opts *ViewOptions) error {
hostname, _ := cfg.Authentication().DefaultHost()
gitIgnore, err := api.RepoGitIgnoreTemplate(client, hostname, opts.Template)
if err != nil {
if strings.Contains(err.Error(), "HTTP 404") {
return fmt.Errorf("'%s' is not a valid gitignore template. Run `gh repo gitignore list` for options", opts.Template)
var httpErr api.HTTPError
if errors.As(err, &httpErr) {
if httpErr.StatusCode == 404 {
return fmt.Errorf("'%s' is not a valid gitignore template. Run `gh repo gitignore list` for options", opts.Template)
}
}
return err
}

View file

@ -94,8 +94,11 @@ func viewRun(opts *ViewOptions) error {
hostname, _ := cfg.Authentication().DefaultHost()
license, err := api.RepoLicense(client, hostname, opts.License)
if err != nil {
if strings.Contains(err.Error(), "HTTP 404") {
return fmt.Errorf("'%s' is not a valid license template name or SPDX ID.\n\nRun `gh repo license list` to see available commonly used licenses. For even more licenses, visit %s", opts.License, text.DisplayURL("https://choosealicense.com/appendix"))
var httpErr api.HTTPError
if errors.As(err, &httpErr) {
if httpErr.StatusCode == 404 {
return fmt.Errorf("'%s' is not a valid license template name or SPDX ID.\n\nRun `gh repo license list` to see available commonly used licenses. For even more licenses, visit %s", opts.License, text.DisplayURL("https://choosealicense.com/appendix"))
}
}
return err
}