-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Improve error message handling and avoid memory leaks (#100)
- Loading branch information
1 parent
e1a2da8
commit bcca50e
Showing
15 changed files
with
208 additions
and
284 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
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,30 @@ | ||
package monitor | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/jmespath/go-jmespath" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
func errorFromResponse(response *http.Response) error { | ||
var data interface{} | ||
err := json.NewDecoder(response.Body).Decode(&data) | ||
if err != nil { | ||
return errors.New(response.Status) | ||
} | ||
|
||
search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data) | ||
if err != nil { | ||
return errors.New(response.Status) | ||
} | ||
|
||
if searchArray, ok := search.([]interface{}); ok { | ||
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", ")) | ||
} | ||
|
||
return errors.New(cast.ToString(search)) | ||
} |
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,30 @@ | ||
package secure | ||
|
||
import ( | ||
"encoding/json" | ||
"errors" | ||
"net/http" | ||
"strings" | ||
|
||
"github.com/jmespath/go-jmespath" | ||
"github.com/spf13/cast" | ||
) | ||
|
||
func errorFromResponse(response *http.Response) error { | ||
var data interface{} | ||
err := json.NewDecoder(response.Body).Decode(&data) | ||
if err != nil { | ||
return errors.New(response.Status) | ||
} | ||
|
||
search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data) | ||
if err != nil { | ||
return errors.New(response.Status) | ||
} | ||
|
||
if searchArray, ok := search.([]interface{}); ok { | ||
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", ")) | ||
} | ||
|
||
return errors.New(cast.ToString(search)) | ||
} |
Oops, something went wrong.