Skip to content

Commit

Permalink
Fix lint errors around wrapped errors.
Browse files Browse the repository at this point in the history
  • Loading branch information
cudevmaxwell committed Apr 8, 2021
1 parent f7cb6f7 commit 8834086
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
8 changes: 6 additions & 2 deletions almaerror.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ package main

import (
"encoding/xml"
"errors"
"fmt"
)

// ErrAPIError is an error which is used when the Alma API returns an error.
var ErrAPIError = errors.New("an API error occurred")

// APIError is a struct which stores the data from Alma API errors.
type APIError struct {
XMLName xml.Name `xml:"web_service_result"`
Expand All @@ -25,7 +29,7 @@ type APIError struct {
// error codes which mean the same thing.
func (e *APIError) Collapse() error {
if len(e.ErrorList) > 0 {
return fmt.Errorf("%v: %v", e.ErrorList[0].Error.ErrorCode, e.ErrorList[0].Error.ErrorMessage)
return fmt.Errorf("%w: %v - %v", ErrAPIError, e.ErrorList[0].Error.ErrorCode, e.ErrorList[0].Error.ErrorMessage)
}
return fmt.Errorf("unknown error")
return fmt.Errorf("%w: unknown error", ErrAPIError)
}
20 changes: 10 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func RetrySubmitJob(maxRetries int, url *url.URL, timeout int, key string, param
}
return jobInstanceLink, nil
}
return "", fmt.Errorf("maximum number of retries reached")
return "", fmt.Errorf("%w: maximum number of retries reached", ErrAPIError)
}

// SubmitJob sends a POST HTTP request to the Alma API to execute the job.
Expand Down Expand Up @@ -289,19 +289,19 @@ func SubmitJob(url *url.URL, timeout int, key string, params AlmaJob) (jobInstan
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
if err != nil {
return "", fmt.Errorf("alma API request failed, HTTP status %v, couldn't read body: %v", resp.Status, err)
return "", fmt.Errorf("alma API request failed, HTTP status %v, couldn't read body: %w", resp.Status, err)
}
return "", fmt.Errorf("alma API request failed, HTTP status %v, %v", resp.Status, apiErr.Collapse())
return "", fmt.Errorf("alma API request failed, HTTP status %v, %w", resp.Status, apiErr.Collapse())
}

// If the Status != OK, there was an error we didn't catch yet.
if resp.StatusCode != 200 {
bodyBytes, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", fmt.Errorf("alma API request failed: %v couldn't read body: %v", resp.Status, err)
return "", fmt.Errorf("alma API request failed: %v couldn't read body: %w", resp.Status, err)
}
return "", fmt.Errorf("alma API request failed: %v - %v", resp.Status, string(bodyBytes))
return "", fmt.Errorf("alma API request failed: %v - %v: %w", resp.Status, string(bodyBytes), ErrAPIError)
}

// Decode the job and return the job instance ID.
Expand Down Expand Up @@ -330,7 +330,7 @@ func MonitorJobInstance(url *url.URL, timeout int, key string) (instance *AlmaJo
}
time.Sleep(30 * time.Second)
}
return instance, fmt.Errorf("job monitor has been running for 23 hours, exiting")
return instance, fmt.Errorf("%w: job monitor has been running for 23 hours, exiting", ErrAPIError)
}

// GetJobInstance sends a GET HTTP request to the Alma API to get job instance data.
Expand Down Expand Up @@ -375,19 +375,19 @@ func GetJobInstance(url *url.URL, timeout int, key string) (instance *AlmaJobIns
_, _ = io.Copy(io.Discard, resp.Body)
resp.Body.Close()
if err != nil {
return instance, fmt.Errorf("alma API request failed, HTTP status %v, couldn't read body: %v", resp.Status, err)
return instance, fmt.Errorf("alma API request failed, HTTP status %v, couldn't read body: %w", resp.Status, err)
}
return instance, fmt.Errorf("alma API request failed, HTTP status %v, %v", resp.Status, apiErr.Collapse())
return instance, fmt.Errorf("alma API request failed, HTTP status %v, %w", resp.Status, apiErr.Collapse())
}

// If the Status != OK, there was an error we didn't catch yet.
if resp.StatusCode != 200 {
bodyBytes, err := io.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return instance, fmt.Errorf("alma API request failed: %v couldn't read body: %v", resp.Status, err)
return instance, fmt.Errorf("alma API request failed: %v couldn't read body: %w", resp.Status, err)
}
return instance, fmt.Errorf("alma API request failed: %v - %v", resp.Status, string(bodyBytes))
return instance, fmt.Errorf("alma API request failed: %v - %v: %w", resp.Status, string(bodyBytes), ErrAPIError)
}

// Decode the job and return the job instance ID.
Expand Down

0 comments on commit 8834086

Please sign in to comment.