ExecuteResult.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. message=exception.toString();
  59. }
  60. public long getUseTime() {
  61. return useTime;
  62. }
  63. public void setUseTime(long useTime) {
  64. this.useTime = useTime;
  65. }
  66. @Override
  67. public String toString() {
  68. return String.valueOf(this.getResult());
  69. }
  70. public Object get() {
  71. return result;
  72. }
  73. @JsonIgnore
  74. public JSON getIfSuccess() throws Exception {
  75. if (!success) {
  76. if (exception != null) {
  77. throw exception;
  78. } else{
  79. throw new ScriptException(message);
  80. }
  81. }
  82. return result;
  83. }
  84. public JSON getIfSuccess(JSON defaultValue) {
  85. if (!success) {
  86. return defaultValue;
  87. }
  88. return result;
  89. }
  90. public Object getIfSuccess(Supplier<?> supplier) {
  91. if (!success) {
  92. return supplier.get();
  93. }
  94. return result;
  95. }
  96. }