-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attach the Request ID to relevant SDK exceptions (#23)
* Attach the Request ID to relevant SDK exceptions * fix build * typo
- Loading branch information
1 parent
085b486
commit 81ed1a1
Showing
5 changed files
with
126 additions
and
19 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,47 @@ | ||
package workos | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
|
||
"github.com/tidwall/gjson" | ||
) | ||
|
||
// TryGetHTTPError returns an error when the http response contains invalid | ||
// status code. | ||
func TryGetHTTPError(r *http.Response) error { | ||
if r.StatusCode >= 200 && r.StatusCode < 300 { | ||
return nil | ||
} | ||
|
||
var msg string | ||
|
||
body, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
msg = err.Error() | ||
} else if m := gjson.GetBytes(body, "message").Str; m != "" { | ||
msg = m | ||
} else { | ||
msg = string(body) | ||
} | ||
|
||
return HTTPError{ | ||
Code: r.StatusCode, | ||
Status: r.Status, | ||
RequestID: r.Header.Get("X-Request-ID"), | ||
Message: msg, | ||
} | ||
} | ||
|
||
// HTTPError represents an http error. | ||
type HTTPError struct { | ||
Code int | ||
Status string | ||
RequestID string | ||
Message string | ||
} | ||
|
||
func (e HTTPError) Error() string { | ||
return fmt.Sprintf("%s: request id %q: %s", e.Status, e.RequestID, e.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,71 @@ | ||
package workos | ||
|
||
import ( | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestGetHTTPErrorWithJSONPayload(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
rec.Header().Set("X-Request-ID", "GOrOXx") | ||
rec.WriteHeader(http.StatusUnauthorized) | ||
rec.WriteString(`{"message":"unauthorized"}`) | ||
|
||
err := TryGetHTTPError(rec.Result()) | ||
require.Error(t, err) | ||
|
||
httperr := err.(HTTPError) | ||
require.Equal(t, http.StatusUnauthorized, httperr.Code) | ||
require.Equal(t, "401 Unauthorized", httperr.Status) | ||
require.Equal(t, "GOrOXx", httperr.RequestID) | ||
require.Equal(t, "unauthorized", httperr.Message) | ||
|
||
t.Log(httperr) | ||
} | ||
|
||
func TestGetHTTPErrorWithTextPayload(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
rec.Header().Set("X-Request-ID", "GOrOXx") | ||
rec.WriteHeader(http.StatusUnauthorized) | ||
rec.WriteString("unauthorized msg") | ||
|
||
err := TryGetHTTPError(rec.Result()) | ||
require.Error(t, err) | ||
|
||
httperr := err.(HTTPError) | ||
require.Equal(t, http.StatusUnauthorized, httperr.Code) | ||
require.Equal(t, "401 Unauthorized", httperr.Status) | ||
require.Equal(t, "GOrOXx", httperr.RequestID) | ||
require.Equal(t, "unauthorized msg", httperr.Message) | ||
|
||
t.Log(httperr) | ||
} | ||
|
||
func TestGetHTTPErrorWithoutRequestID(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
rec.WriteHeader(http.StatusUnauthorized) | ||
rec.WriteString(`{"message":"unauthorized"}`) | ||
|
||
err := TryGetHTTPError(rec.Result()) | ||
require.Error(t, err) | ||
|
||
httperr := err.(HTTPError) | ||
require.Equal(t, http.StatusUnauthorized, httperr.Code) | ||
require.Equal(t, "401 Unauthorized", httperr.Status) | ||
require.Empty(t, httperr.RequestID) | ||
require.Equal(t, "unauthorized", httperr.Message) | ||
|
||
t.Log(httperr) | ||
} | ||
|
||
func TestGetHTTPErrorNoError(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
rec.Header().Set("X-Request-ID", "GOrOXx") | ||
rec.WriteHeader(http.StatusOK) | ||
|
||
err := TryGetHTTPError(rec.Result()) | ||
require.NoError(t, err) | ||
} |