This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
Handle wac template parse errors #271
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
17a3f56
Handle wac template errors
Emyrk 377d5b2
Linting
Emyrk 12ea48d
Add explict comments
Emyrk b0c83ef
linting: Thanks for the period reminder godot...
Emyrk 417cdbd
Add verbose handling
Emyrk 649b468
Handle verbose errors
Emyrk 463a882
godot will be the bane of me
Emyrk d5adf70
Rephrase from code review suggestion
Emyrk fe5a60c
Handle precondition errors
Emyrk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package cmd | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"golang.org/x/xerrors" | ||
|
||
"cdr.dev/coder-cli/coder-sdk" | ||
"cdr.dev/coder-cli/pkg/clog" | ||
) | ||
|
||
// handleAPIError attempts to convert an api error into a more detailed clog error. | ||
// If it cannot, it will return the original error. | ||
func handleAPIError(origError error) error { | ||
var httpError *coder.HTTPError | ||
if !xerrors.As(origError, &httpError) { | ||
return origError // Return the original | ||
} | ||
|
||
ae, err := httpError.Payload() | ||
if err != nil { | ||
return origError // Return the original | ||
} | ||
|
||
switch ae.Err.Code { | ||
case "wac_template": // template parse errors | ||
type templatePayload struct { | ||
ErrorType string `json:"error_type"` | ||
Msgs []string `json:"messages"` | ||
} | ||
|
||
var p templatePayload | ||
err := json.Unmarshal(ae.Err.Details, &p) | ||
if err != nil { | ||
return origError | ||
} | ||
|
||
return clog.Error(p.ErrorType, p.Msgs...) | ||
case "verbose": | ||
type verbosePayload struct { | ||
Verbose string `json:"verbose"` | ||
} | ||
var p verbosePayload | ||
err := json.Unmarshal(ae.Err.Details, &p) | ||
if err != nil { | ||
return origError | ||
} | ||
|
||
return clog.Error(origError.Error(), p.Verbose) | ||
case "precondition": | ||
type preconditionPayload struct { | ||
Error string `json:"error"` | ||
Message string `json:"message"` | ||
Solution string `json:"solution"` | ||
} | ||
|
||
var p preconditionPayload | ||
err := json.Unmarshal(ae.Err.Details, &p) | ||
if err != nil { | ||
return origError | ||
} | ||
|
||
return clog.Error(fmt.Sprintf("Precondition Error : Status Code=%d", httpError.StatusCode), | ||
p.Message, | ||
clog.BlankLine, | ||
clog.Tipf(p.Solution)) | ||
} | ||
|
||
return origError // Return the original | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We decode the
re.Response.Body
on.Error()
. I didn't feel like trying to fix that behavior, but I need to cache the decoded payload as .Error() can be called more than once, and then the second one fails. So I split.Error()
in to.Payload
so I can extract the payload and not change any current behavior.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.
oh nice catch, I also think we had this buggy code duplicated in the monorepo. merged a patch there last week.
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.
It still feels a little odd, but I didn't want to refactor how errors worked as I only touched small areas of the cli so far.
So this works for now.