Skip to content

Commit

Permalink
feat: ThingMessageReply增加assertSuccess方法
Browse files Browse the repository at this point in the history
  • Loading branch information
zhou-hao committed Aug 15, 2024
1 parent 6879631 commit af558e7
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions src/main/java/org/jetlinks/core/message/ThingMessageReply.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.jetlinks.core.message;

import org.jetlinks.core.enums.ErrorCode;
import org.jetlinks.core.exception.DeviceOperationException;
import org.jetlinks.core.utils.SerializeUtils;

import javax.annotation.Nonnull;
Expand All @@ -25,8 +26,8 @@ public interface ThingMessageReply extends ThingMessage {
//设置失败
default ThingMessageReply error(String errorCode, String msg) {
return success(false)
.code(errorCode)
.message(msg);
.code(errorCode)
.message(msg);
}

//设置失败
Expand Down Expand Up @@ -88,4 +89,17 @@ default void readExternal(ObjectInput in) throws IOException, ClassNotFoundExcep
this.code(SerializeUtils.readNullableUTF(in));
this.message(SerializeUtils.readNullableUTF(in));
}

/**
* 断言请求是否成功,如果失败则抛出异常
*
* @throws DeviceOperationException error
* @since 1.2.2
*/
default void assertSuccess() throws DeviceOperationException {
if (!isSuccess()) {
throw new DeviceOperationException
.NoStackTrace(ErrorCode.of(getCode()).orElse(ErrorCode.UNKNOWN), getMessage());
}
}
}

0 comments on commit af558e7

Please sign in to comment.