forked from apache/iceberg
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST: implement handling of OAuth error responses followup (apache#5820)
* WIP error handling for OAuth * cleanup * tests * handle non-oauth errors in oauth * add comment * allow null fields * more tests * more cleanup * remove unneeded precondition checks * Fix test * use assert4j * Keep client API the same * Keep ErrorResponse the same * PR feedback * handle invalid error response body format
- Loading branch information
Showing
10 changed files
with
305 additions
and
62 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
core/src/main/java/org/apache/iceberg/rest/ErrorHandler.java
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,27 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.iceberg.rest; | ||
|
||
import java.util.function.Consumer; | ||
import org.apache.iceberg.rest.responses.ErrorResponse; | ||
|
||
public abstract class ErrorHandler implements Consumer<ErrorResponse> { | ||
|
||
public abstract ErrorResponse parseResponse(int code, String json); | ||
} |
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
61 changes: 61 additions & 0 deletions
61
core/src/main/java/org/apache/iceberg/rest/responses/OAuthErrorResponseParser.java
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,61 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.iceberg.rest.responses; | ||
|
||
import com.fasterxml.jackson.databind.JsonNode; | ||
import java.io.IOException; | ||
import java.io.UncheckedIOException; | ||
import org.apache.iceberg.relocated.com.google.common.base.Preconditions; | ||
import org.apache.iceberg.util.JsonUtil; | ||
|
||
public class OAuthErrorResponseParser { | ||
|
||
private OAuthErrorResponseParser() {} | ||
|
||
private static final String ERROR = "error"; | ||
private static final String ERROR_DESCRIPTION = "error_description"; | ||
|
||
/** | ||
* Read OAuthErrorResponse from a JSON string. | ||
* | ||
* @param json a JSON string of an OAuthErrorResponse | ||
* @return an OAuthErrorResponse object | ||
*/ | ||
public static ErrorResponse fromJson(int code, String json) { | ||
try { | ||
return fromJson(code, JsonUtil.mapper().readValue(json, JsonNode.class)); | ||
} catch (IOException e) { | ||
throw new UncheckedIOException("Failed to read JSON string: " + json, e); | ||
} | ||
} | ||
|
||
public static ErrorResponse fromJson(int code, JsonNode jsonNode) { | ||
Preconditions.checkArgument( | ||
jsonNode != null && jsonNode.isObject(), | ||
"Cannot parse error response from non-object value: %s", | ||
jsonNode); | ||
String error = JsonUtil.getString(ERROR, jsonNode); | ||
String errorDescription = JsonUtil.getStringOrNull(ERROR_DESCRIPTION, jsonNode); | ||
return ErrorResponse.builder() | ||
.responseCode(code) | ||
.withType(error) | ||
.withMessage(errorDescription) | ||
.build(); | ||
} | ||
} |
Oops, something went wrong.