Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/main/java/com/qingstor/sdk/exception/QSException.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,10 @@ public <T extends OutputModel> 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);
}
}
}
Expand Down Expand Up @@ -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();
Expand All @@ -124,11 +125,13 @@ private <T extends OutputModel> 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);
}
}

Expand Down
65 changes: 65 additions & 0 deletions src/test/java/com/qingstor/sdk/exception/QSExceptionTest.java
Original file line number Diff line number Diff line change
@@ -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());
}
}
Loading