ExecuteResult.java 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.coffee.common.exception;
  2. import cn.hutool.json.JSON;
  3. import com.fasterxml.jackson.annotation.JsonIgnore;
  4. import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
  5. import io.swagger.annotations.ApiModel;
  6. import io.swagger.annotations.ApiModelProperty;
  7. import lombok.Data;
  8. import javax.script.ScriptException;
  9. import java.util.function.Supplier;
  10. @Data
  11. @ApiModel("脚本执行结果")
  12. public class ExecuteResult {
  13. @ApiModelProperty("脚本是否执行成功")
  14. private boolean success;
  15. @ApiModelProperty("脚本输入参数")
  16. private String input;
  17. @ApiModelProperty("脚本输出")
  18. private JSON result;
  19. @ApiModelProperty("错误消息")
  20. private String message;
  21. @ApiModelProperty("错误类型")
  22. @JsonIgnore
  23. private transient Exception exception;
  24. @ApiModelProperty("脚本运行时间")
  25. private long useTime;
  26. public boolean isSuccess() {
  27. return success;
  28. }
  29. public void setSuccess(boolean success) {
  30. this.success = success;
  31. }
  32. /**
  33. * use {@link this#get()} or {@link this#getIfSuccess()}
  34. *
  35. * @return
  36. */
  37. @Deprecated
  38. public JSON getResult() {
  39. return result;
  40. }
  41. public void setResult(JSON result) {
  42. this.result = result;
  43. }
  44. public String getMessage() {
  45. if (message == null && exception != null) {
  46. message = exception.getMessage();
  47. }
  48. return message;
  49. }
  50. public void setMessage(String message) {
  51. this.message = message;
  52. }
  53. public Exception getException() {
  54. return exception;
  55. }
  56. public void setException(Exception exception) {
  57. this.exception = exception;
  58. }
  59. public long getUseTime() {
  60. return useTime;
  61. }
  62. public void setUseTime(long useTime) {
  63. this.useTime = useTime;
  64. }
  65. @Override
  66. public String toString() {
  67. return String.valueOf(this.getResult());
  68. }
  69. public Object get() {
  70. return result;
  71. }
  72. @JsonIgnore
  73. public JSON getIfSuccess() throws Exception {
  74. if (!success) {
  75. if (exception != null) {
  76. throw exception;
  77. } else{
  78. throw new ScriptException(message);
  79. }
  80. }
  81. return result;
  82. }
  83. public JSON getIfSuccess(JSON defaultValue) {
  84. if (!success) {
  85. return defaultValue;
  86. }
  87. return result;
  88. }
  89. public Object getIfSuccess(Supplier<?> supplier) {
  90. if (!success) {
  91. return supplier.get();
  92. }
  93. return result;
  94. }
  95. }