package com.coffee.common.exception; import cn.hutool.json.JSON; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; import javax.script.ScriptException; import java.util.function.Supplier; @Data @ApiModel("脚本执行结果") public class ExecuteResult { @ApiModelProperty("脚本是否执行成功") private boolean success; @ApiModelProperty("脚本输入参数") private String input; @ApiModelProperty("脚本输出") private JSON result; @ApiModelProperty("错误消息") private String message; @ApiModelProperty("错误类型") @JsonIgnore private transient Exception exception; @ApiModelProperty("脚本运行时间") private long useTime; public boolean isSuccess() { return success; } public void setSuccess(boolean success) { this.success = success; } /** * use {@link this#get()} or {@link this#getIfSuccess()} * * @return */ @Deprecated public JSON getResult() { return result; } public void setResult(JSON result) { this.result = result; } public String getMessage() { if (message == null && exception != null) { message = exception.getMessage(); } return message; } public void setMessage(String message) { this.message = message; } public Exception getException() { return exception; } public void setException(Exception exception) { this.exception = exception; } public long getUseTime() { return useTime; } public void setUseTime(long useTime) { this.useTime = useTime; } @Override public String toString() { return String.valueOf(this.getResult()); } public Object get() { return result; } @JsonIgnore public JSON getIfSuccess() throws Exception { if (!success) { if (exception != null) { throw exception; } else{ throw new ScriptException(message); } } return result; } public JSON getIfSuccess(JSON defaultValue) { if (!success) { return defaultValue; } return result; } public Object getIfSuccess(Supplier supplier) { if (!success) { return supplier.get(); } return result; } }