-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve the error handling to include the status code and error detai…
…ls (#13) * improve the error handling to include the status code and error details; handle fully-qualified ids in v5 * start a changelog
- Loading branch information
1 parent
f4c7165
commit 3936e4e
Showing
18 changed files
with
290 additions
and
111 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# v0.2.0 | ||
|
||
* Adds support for structured error responses from the Conjur v5 server, using the struct `conjurapi.ConjurError`. This is a backwards incompatible change. | ||
* All API methods accept fully qualified object ids in v5 mode. This is a backwards compatible bug fix. | ||
* API methods which do not work in v4 mode return an appropriate error message. This is a backwards compatible bug fix. | ||
|
||
# v0.1.0 | ||
|
||
* Initial version |
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
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,45 @@ | ||
package response | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type ConjurError struct { | ||
Code int | ||
Message string | ||
Details *ConjurErrorDetails `json:"error"` | ||
} | ||
|
||
type ConjurErrorDetails struct { | ||
Message string | ||
Code string | ||
Target string | ||
Details map[string]interface{} | ||
} | ||
|
||
func NewConjurError(resp *http.Response) (error) { | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
cerr := ConjurError{} | ||
cerr.Code = resp.StatusCode | ||
err = json.Unmarshal(body, &cerr) | ||
if err != nil { | ||
cerr.Message = strings.TrimSpace(string(body)) | ||
} | ||
return &cerr | ||
} | ||
|
||
func (self *ConjurError) Error() string { | ||
if self.Details != nil && self.Details.Message != "" { | ||
return self.Details.Message | ||
} else { | ||
return self.Message | ||
} | ||
} |
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 response | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
func readBody(resp *http.Response) ([]byte, error) { | ||
defer resp.Body.Close() | ||
|
||
responseText, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return responseText, err | ||
} | ||
|
||
func SecretDataResponse(resp *http.Response) ([]byte, error) { | ||
if resp.StatusCode < 300 { | ||
return readBody(resp) | ||
} else { | ||
return nil, NewConjurError(resp) | ||
} | ||
} | ||
|
||
func JSONResponse(resp *http.Response, obj interface{}) (error) { | ||
if resp.StatusCode < 300 { | ||
body, err := readBody(resp) | ||
if err != nil { | ||
return err | ||
} | ||
return json.Unmarshal(body, obj) | ||
} else { | ||
return NewConjurError(resp) | ||
} | ||
} | ||
|
||
func EmptyResponse(resp *http.Response) (error) { | ||
if resp.StatusCode < 300 { | ||
return nil | ||
} else { | ||
return NewConjurError(resp) | ||
} | ||
} |
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
Oops, something went wrong.