-
Notifications
You must be signed in to change notification settings - Fork 344
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
throw retryable exception when http response code is retryable
- Loading branch information
1 parent
7192fd5
commit 062eba4
Showing
5 changed files
with
128 additions
and
27 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
cdap-api/src/main/java/io/cdap/cdap/api/service/ServiceException.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,76 @@ | ||
/* | ||
* Copyright © 2024 Cask Data, Inc. | ||
* | ||
* Licensed 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 io.cdap.cdap.api.service; | ||
|
||
import java.io.IOException; | ||
import java.net.HttpURLConnection; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* Similar to {@link ServiceUnavailableException}, but responds with a custom error code. | ||
*/ | ||
public class ServiceException extends ServiceUnavailableException { | ||
|
||
public static final List<Integer> HTTP_SERVER_ERROR_CODES = Collections.unmodifiableList( | ||
Arrays.asList(HttpURLConnection.HTTP_BAD_GATEWAY, | ||
HttpURLConnection.HTTP_GATEWAY_TIMEOUT, | ||
HttpURLConnection.HTTP_INTERNAL_ERROR, | ||
HttpURLConnection.HTTP_UNAVAILABLE)); | ||
private final int statusCode; | ||
private final String jsonDetails; | ||
|
||
/** | ||
* Constructs {@link ServiceException}. | ||
* | ||
* @param serviceName the canonical name of service | ||
* @param message the message | ||
* @param jsonDetails the json details | ||
* @param cause the cause {@link Throwable} | ||
* @param statusCode the status code | ||
*/ | ||
public ServiceException(String serviceName, String message, @Nullable String jsonDetails, | ||
@Nullable Throwable cause, int statusCode) throws IOException { | ||
super(serviceName, message, cause); | ||
|
||
if (!HTTP_SERVER_ERROR_CODES.contains(statusCode)) { | ||
throw new IOException( | ||
"Cannot instantiate Service Exception, it is not a retryable response code"); | ||
} | ||
|
||
this.statusCode = statusCode; | ||
this.jsonDetails = jsonDetails; | ||
} | ||
|
||
/** | ||
* Returns the service name. | ||
*/ | ||
@Nullable | ||
public String getJsonDetails() { | ||
return jsonDetails; | ||
} | ||
|
||
/** | ||
* Returns the http status code. | ||
*/ | ||
@Override | ||
public int getStatusCode() { | ||
return statusCode; | ||
} | ||
} |
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