Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Always return code or error in responses as ErrorCode when present #203

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pkg/workos_errors/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,23 @@ func getJsonErrorMessage(b []byte, statusCode int) (string, string, []string, []
return string(b), "", nil, nil
}

var errorCode string

if payload.Code != "" {
errorCode = payload.Code
} else {
errorCode = payload.Error
}

if payload.Error != "" && payload.ErrorDescription != "" {
return fmt.Sprintf("%s %s", payload.Error, payload.ErrorDescription), "", nil, nil
return fmt.Sprintf("%s %s", payload.Error, payload.ErrorDescription), errorCode, nil, nil
} else if payload.Message != "" && len(payload.Errors) == 0 {
return payload.Message, "", nil, nil
return payload.Message, errorCode, nil, nil
} else if payload.Message != "" && len(payload.Errors) > 0 {
return payload.Message, payload.Code, payload.Errors, nil
return payload.Message, errorCode, payload.Errors, nil
}

return string(b), "", nil, nil
return string(b), errorCode, nil, nil
}

// HTTPError represents an http error.
Expand Down
28 changes: 24 additions & 4 deletions pkg/workos_errors/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,36 @@ func TestGetHTTPErrorWithJSONPayload(t *testing.T) {
rec.Header().Set("X-Request-ID", "GOrOXx")
rec.Header().Set("Content-Type", "application/json")
rec.WriteHeader(http.StatusUnauthorized)
rec.WriteString(`{"message":"unauthorized", "error": "unauthorized error", "error_description": "unauthorized error description"}`)
rec.WriteString(`{"message":"unauthorized", "error": "unauthorized_error", "error_description": "unauthorized error description"}`)

err := TryGetHTTPError(rec.Result())
require.Error(t, err)

httperr := err.(HTTPError)
require.Equal(t, http.StatusUnauthorized, httperr.Code)
require.Equal(t, "unauthorized_error", httperr.ErrorCode)
require.Equal(t, "401 Unauthorized", httperr.Status)
require.Equal(t, "GOrOXx", httperr.RequestID)
require.Equal(t, "unauthorized error unauthorized error description", httperr.Message)
require.Equal(t, "unauthorized_error unauthorized error description", httperr.Message)

t.Log(httperr)
}

func TestGetHTTPErrorWithBothErrorAndCode(t *testing.T) {
rec := httptest.NewRecorder()
rec.Header().Set("X-Request-ID", "GOrOXx")
rec.Header().Set("Content-Type", "application/json")
rec.WriteHeader(http.StatusUnauthorized)
rec.WriteString(`{"message":"unauthorized", "code": "bad_credentials", "error": "unauthorized_error"}`)

err := TryGetHTTPError(rec.Result())
require.Error(t, err)

httperr := err.(HTTPError)
require.Equal(t, http.StatusUnauthorized, httperr.Code)
require.Equal(t, "bad_credentials", httperr.ErrorCode)
require.Equal(t, "GOrOXx", httperr.RequestID)
require.Equal(t, "unauthorized", httperr.Message)

t.Log(httperr)
}
Expand Down Expand Up @@ -128,7 +148,7 @@ func TestGetHTTPErrorWithoutRequestID(t *testing.T) {
rec := httptest.NewRecorder()
rec.Header().Set("Content-Type", "application/json")
rec.WriteHeader(http.StatusUnauthorized)
rec.WriteString(`{"message":"unauthorized", "error": "unauthorized error", "error_description": "unauthorized error description"}`)
rec.WriteString(`{"message":"unauthorized", "error": "unauthorized_error", "error_description": "unauthorized error description"}`)

err := TryGetHTTPError(rec.Result())
require.Error(t, err)
Expand All @@ -137,7 +157,7 @@ func TestGetHTTPErrorWithoutRequestID(t *testing.T) {
require.Equal(t, http.StatusUnauthorized, httperr.Code)
require.Equal(t, "401 Unauthorized", httperr.Status)
require.Empty(t, httperr.RequestID)
require.Equal(t, "unauthorized error unauthorized error description", httperr.Message)
require.Equal(t, "unauthorized_error unauthorized error description", httperr.Message)

t.Log(httperr)
}
Expand Down