LoginSignup
1
0

More than 1 year has passed since last update.

RESTFul API の例外- ClientHandlerException

Posted at

事前情報

・最初からRESTFul APIを作ったことがない
・すでにあるAPIでやり取りだけ

やりたいものちゃんとエラーハンドリング

例) https://www.programcreek.com/java-api-examples/?class=com.sun.jersey.api.client.ClientHandlerException&method=getCause
↓のコードが理解できなかった

public ChannelFinderException(ClientHandlerException cause) {
    super(cause.getMessage());
    if(cause.getCause() instanceof SocketException) {
        this.setStatus(Status.NOT_FOUND);
    } else {
        this.setStatus(Status.SEE_OTHER);
    }
}

ClientHadnlerExceptionはRuntimeExceptionなのにSocketException IOExceptionのものもチェックしてくるの?(まだgetCauseが見えない)

Java.lang.Object
extended by java.lang.Throwable

extended by java.lang.Exception

extended by java.lang.RuntimeException

extended by com.sun.jersey.api.client.ClientHandlerException

SocketExceptionの場合
image.png

image.png
写真参照)
https://rollbar.com/blog/java-exceptions-hierarchy-explained/#:~:text=The%20class%20at%20the%20top,direct%20subclasses%20%2D%20Exception%20and%20Error.&text=Exceptions%20are%20further%20subdivided%20into,(run%2Dtime)%20exceptions.

例外の継承関係を見たら?instanceofする前に引っかからないのでは?

と思ったが違った→

ex.getCause()
ClientHandlerExceptionのconstrunctorを見ると
A runtime exception thrown by a client handler that signals a failure to process the HTTP request or HTTP response.

ClientHandlerException(java.lang.Throwable cause)(ここからは推測。。。)
がある、つまりAPI側ではエラーをClientHandlerExceptionのエラーを返しているでしょう
それで↓の書き方ができるでしょう

  if(cause.getCause() instanceof SocketException) {

ちょっと。。。推測だらけなので、後でjerseyを使ってrestful api作成した方がいいかも

参照資料
https://www.javadoc.io/doc/com.sun.jersey/jersey-bundle/1.4-ea01/com/sun/jersey/api/client/class-use/ClientHandlerException.html#com.sun.jersey.api.client

指定したJavaの型がサポートされない型の場合,エラーとなり(KDJJ10031-EおよびKDJJ18888-E),ClientHandlerExceptionがスローされます。

戻り値が次に示すJavaの型で,HTTPレスポンスのエンティティボディが使用できないMIMEメディアタイプの場合,エラーとなり(KDJJ10031-EまたはKDJJ18888-E),ClientHandlerExceptionがスローされます。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0