-
Notifications
You must be signed in to change notification settings - Fork 35
Fix error type for non-JSON error responses #758
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
cbd40f3
Fix error type for non-JSON error responses
renaudhartert-db 0239758
Add changelog entry for plain-text error handling fix
renaudhartert-db 144d063
Merge branch 'main' into fix/plain-text-error-handling
renaudhartert-db e9b6d0b
Set non-null error code for non-JSON error responses
renaudhartert-db 8e6cbe9
Fix missing message extraction for HTML error responses
renaudhartert-db File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
65 changes: 65 additions & 0 deletions
65
databricks-sdk-java/src/test/java/com/databricks/sdk/core/error/PlainTextErrorTest.java
This file contains hidden or 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,65 @@ | ||
| package com.databricks.sdk.core.error; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import com.databricks.sdk.core.DatabricksError; | ||
| import com.databricks.sdk.core.error.platform.*; | ||
| import com.databricks.sdk.core.http.Request; | ||
| import com.databricks.sdk.core.http.Response; | ||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| class PlainTextErrorTest { | ||
|
|
||
| @Test | ||
| void plainTextForbiddenReturnsPermissionDenied() { | ||
| DatabricksError error = getError(403, "Forbidden", "Invalid Token"); | ||
| assertInstanceOf(PermissionDenied.class, error); | ||
| assertEquals("Invalid Token", error.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void plainTextUnauthorizedReturnsUnauthenticated() { | ||
| DatabricksError error = getError(401, "Unauthorized", "Bad credentials"); | ||
| assertInstanceOf(Unauthenticated.class, error); | ||
| assertEquals("Bad credentials", error.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void plainTextNotFoundReturnsNotFound() { | ||
| DatabricksError error = getError(404, "Not Found", "no such endpoint"); | ||
| assertInstanceOf(NotFound.class, error); | ||
| assertEquals("no such endpoint", error.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void htmlErrorExtractsPreContent() { | ||
| String html = "<html><body><pre>some error message</pre></body></html>"; | ||
| DatabricksError error = getError(403, "Forbidden", html); | ||
| assertInstanceOf(PermissionDenied.class, error); | ||
| assertEquals("some error message", error.getMessage()); | ||
| } | ||
|
|
||
| @Test | ||
| void emptyBodyFallsBackToStatusCode() { | ||
| Request request = new Request("GET", "https://example.com/api/2.0/clusters/get"); | ||
| Response response = new Response(request, 403, "Forbidden", Collections.emptyMap(), ""); | ||
| DatabricksError error = ApiErrors.getDatabricksError(response); | ||
| assertInstanceOf(PermissionDenied.class, error); | ||
| } | ||
|
|
||
| @Test | ||
| void nullBodyFallsBackToStatusCode() { | ||
| Request request = new Request("GET", "https://example.com/api/2.0/clusters/get"); | ||
| Response response = | ||
| new Response(request, 403, "Forbidden", Collections.emptyMap(), (String) null); | ||
| DatabricksError error = ApiErrors.getDatabricksError(response); | ||
| assertInstanceOf(PermissionDenied.class, error); | ||
| } | ||
|
|
||
| private static DatabricksError getError(int statusCode, String status, String body) { | ||
| Request request = new Request("GET", "https://example.com/api/2.0/clusters/get"); | ||
| Response response = new Response(request, statusCode, status, Collections.emptyMap(), body); | ||
| return ApiErrors.getDatabricksError(response); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.