diff --git a/src/main/java/com/qingstor/sdk/exception/QSException.java b/src/main/java/com/qingstor/sdk/exception/QSException.java index 12b3795c..98745693 100644 --- a/src/main/java/com/qingstor/sdk/exception/QSException.java +++ b/src/main/java/com/qingstor/sdk/exception/QSException.java @@ -25,6 +25,10 @@ public class QSException extends Exception { private String errorMessage; + public QSException(Throwable t) { + super(t); + } + public QSException(String message, Throwable t) { super(message, t); } @@ -60,9 +64,16 @@ public void setErrorMessage(String errorMessage) { @Override public String getMessage() { String cause = super.getMessage(); - if (getCause() != null) { + if (cause == null && getCause() != null) { + cause = + getCause().getMessage() != null + ? getCause().getMessage() + : getCause().toString(); + } else if (cause != null && getCause() != null && getCause().getMessage() != null) { cause += "\n" + getCause().getMessage(); } - return String.format("Error Code: %s; Error Message: %s", getErrorCode(), cause); + return String.format( + "Error Code: %s; Error Message: %s", + getErrorCode(), cause != null ? cause : "Unknown"); } } diff --git a/src/main/java/com/qingstor/sdk/request/QSOkHttpRequestClient.java b/src/main/java/com/qingstor/sdk/request/QSOkHttpRequestClient.java index 563e9d9a..a29fb07a 100644 --- a/src/main/java/com/qingstor/sdk/request/QSOkHttpRequestClient.java +++ b/src/main/java/com/qingstor/sdk/request/QSOkHttpRequestClient.java @@ -75,9 +75,10 @@ public T requestAction( QSConstant.REQUEST_ERROR_CANCELLED, e.getMessage(), model); return model; } else { - e.printStackTrace(); - log.error(e.getMessage()); - throw new QSException(e.getMessage()); + log.error("Failed to execute request action", e); + String message = + e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(); + throw new QSException(message, e); } } } @@ -105,7 +106,7 @@ public void onResponse(Call call, Response response) { callBack.onAPIResponse(m); } } catch (Exception e) { - log.error(e.getMessage()); + log.error("Failed to parse response", e); onOkhttpFailure(e, callBack, outputClass); } finally { response.close(); @@ -124,11 +125,13 @@ private void onOkhttpFailure( if (e instanceof CancellationHandler.CancellationException) errorCode = QSConstant.REQUEST_ERROR_CANCELLED; // Cancelled by users. T m = QSParamInvokeUtil.getOutputModel(outputClass); - fillResponseCallbackModel(errorCode, e.getMessage(), m); + String message = + e.getMessage() != null ? e.getMessage() : e.getClass().getSimpleName(); + fillResponseCallbackModel(errorCode, message, m); callBack.onAPIResponse(m); } } catch (Exception ex) { - log.error(ex.getMessage()); + log.error("Failed in onOkhttpFailure", ex); } } diff --git a/src/test/java/com/qingstor/sdk/exception/QSExceptionTest.java b/src/test/java/com/qingstor/sdk/exception/QSExceptionTest.java new file mode 100644 index 00000000..633754d4 --- /dev/null +++ b/src/test/java/com/qingstor/sdk/exception/QSExceptionTest.java @@ -0,0 +1,65 @@ +/* + * Copyright (C) 2021 Yunify, Inc. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this work except in compliance with the License. + * You may obtain a copy of the License in the LICENSE file, or 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 com.qingstor.sdk.exception; + +import java.io.EOFException; +import org.junit.Assert; +import org.junit.Test; + +public class QSExceptionTest { + + @Test + public void testMessageWithMessageOnly() { + QSException e = new QSException("test error"); + Assert.assertEquals("Error Code: 0; Error Message: test error", e.getMessage()); + } + + @Test + public void testMessageWithMessageAndCause() { + Exception cause = new Exception("cause error"); + QSException e = new QSException("wrapper error", cause); + Assert.assertEquals( + "Error Code: 0; Error Message: wrapper error\ncause error", e.getMessage()); + } + + @Test + public void testMessageWithCauseHavingNullMessage() { + EOFException cause = new EOFException(); + QSException e = new QSException("EOFException", cause); + Assert.assertEquals("Error Code: 0; Error Message: EOFException", e.getMessage()); + } + + @Test + public void testMessageWithNullMessageAndCause() { + EOFException cause = new EOFException(); + QSException e = new QSException(null, cause); + Assert.assertEquals("Error Code: 0; Error Message: java.io.EOFException", e.getMessage()); + } + + @Test + public void testMessageWithThrowableConstructor() { + NullPointerException cause = new NullPointerException(); + QSException e = new QSException(cause); + Assert.assertEquals( + "Error Code: 0; Error Message: java.lang.NullPointerException", e.getMessage()); + } + + @Test + public void testMessageWithNullMessageAndNullCause() { + QSException e = new QSException((String) null); + Assert.assertEquals("Error Code: 0; Error Message: Unknown", e.getMessage()); + } +}