Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var (
RunnerExistsError = scalesetError("runner exists")
JobStillRunningError = scalesetError("job still running")
MessageQueueTokenExpiredError = scalesetError("message queue token expired")
// Top level errors carying the http status code meanings.
BadRequest = scalesetError("bad request")
NotFound = scalesetError("not found")
Unauthorized = scalesetError("unauthorized")
)

type actionsExceptionError struct {
Expand Down Expand Up @@ -87,12 +91,25 @@ func newRequestResponseError(req *http.Request, resp *http.Response, err error)

switch {
case strings.Contains(exception.ExceptionName, "AgentExistsException"):
return fmt.Errorf("%s: %w: %s", sb.String(), RunnerExistsError, exception.Message)
return wrapResponseErrorType(resp, fmt.Errorf("%s: %w: %s", sb.String(), RunnerExistsError, exception.Message))
case strings.Contains(exception.ExceptionName, "AgentNotFoundException"):
return fmt.Errorf("%s: %w: %s", sb.String(), RunnerNotFoundError, exception.Message)
return wrapResponseErrorType(resp, fmt.Errorf("%s: %w: %s", sb.String(), RunnerNotFoundError, exception.Message))
case strings.Contains(exception.ExceptionName, "JobStillRunningException"):
return fmt.Errorf("%s: %w: %s", sb.String(), JobStillRunningError, exception.Message)
return wrapResponseErrorType(resp, fmt.Errorf("%s: %w: %s", sb.String(), JobStillRunningError, exception.Message))
default:
return fmt.Errorf("%s: %w: %w", sb.String(), err, exception)
return wrapResponseErrorType(resp, fmt.Errorf("%s: %w: %w", sb.String(), err, exception))
}
}

func wrapResponseErrorType(resp *http.Response, err error) error {
switch resp.StatusCode {
case http.StatusBadRequest:
return fmt.Errorf("%w: %w", BadRequest, err)
case http.StatusUnauthorized:
return fmt.Errorf("%w: %w", Unauthorized, err)
case http.StatusNotFound:
return fmt.Errorf("%w: %w", NotFound, err)
default:
return err
}
}
Loading