-
Notifications
You must be signed in to change notification settings - Fork 467
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* extract resperr.go * extract method prepareRequest() * reset token inside mutex
- Loading branch information
Showing
4 changed files
with
72 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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,46 @@ | ||
package apiclient | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
|
||
"github.com/crowdsecurity/go-cs-lib/ptr" | ||
|
||
"github.com/crowdsecurity/crowdsec/pkg/models" | ||
) | ||
|
||
type ErrorResponse struct { | ||
models.ErrorResponse | ||
} | ||
|
||
func (e *ErrorResponse) Error() string { | ||
err := fmt.Sprintf("API error: %s", *e.Message) | ||
if len(e.Errors) > 0 { | ||
err += fmt.Sprintf(" (%s)", e.Errors) | ||
} | ||
|
||
return err | ||
} | ||
|
||
// CheckResponse verifies the API response and builds an appropriate Go error if necessary. | ||
func CheckResponse(r *http.Response) error { | ||
if c := r.StatusCode; 200 <= c && c <= 299 || c == 304 { | ||
return nil | ||
} | ||
|
||
ret := &ErrorResponse{} | ||
|
||
data, err := io.ReadAll(r.Body) | ||
if err != nil || len(data) == 0 { | ||
ret.Message = ptr.Of(fmt.Sprintf("http code %d, no error message", r.StatusCode)) | ||
return ret | ||
} | ||
|
||
if err := json.Unmarshal(data, ret); err != nil { | ||
return fmt.Errorf("http code %d, invalid body: %w", r.StatusCode, err) | ||
} | ||
|
||
return ret | ||
} |
This file contains 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