|
15 | 15 | */ |
16 | 16 | package io.serverlessworkflow.fluent.spec; |
17 | 17 |
|
| 18 | +import io.serverlessworkflow.api.types.Error; |
| 19 | +import io.serverlessworkflow.api.types.ErrorDetails; |
| 20 | +import io.serverlessworkflow.api.types.ErrorTitle; |
| 21 | +import io.serverlessworkflow.api.types.ErrorType; |
| 22 | +import io.serverlessworkflow.api.types.UriTemplate; |
18 | 23 | import io.serverlessworkflow.api.types.Use; |
19 | 24 | import io.serverlessworkflow.api.types.UseAuthentications; |
| 25 | +import io.serverlessworkflow.api.types.UseErrors; |
| 26 | +import java.net.URI; |
| 27 | +import java.net.URISyntaxException; |
20 | 28 | import java.util.List; |
21 | 29 | import java.util.function.Consumer; |
22 | 30 |
|
@@ -47,9 +55,81 @@ public UseBuilder authentications(Consumer<UseAuthenticationsBuilder> authentica |
47 | 55 | return this; |
48 | 56 | } |
49 | 57 |
|
50 | | - // TODO: implement the remaining `use` attributes |
| 58 | + public UseBuilder errors(Consumer<UseErrorsBuilder> errorsConsumer) { |
| 59 | + final UseErrorsBuilder builder = new UseErrorsBuilder(); |
| 60 | + errorsConsumer.accept(builder); |
| 61 | + final UseErrors built = builder.build(); |
| 62 | + if (this.use.getErrors() == null) { |
| 63 | + this.use.setErrors(new UseErrors()); |
| 64 | + } |
| 65 | + this.use.getErrors().getAdditionalProperties().putAll(built.getAdditionalProperties()); |
| 66 | + return this; |
| 67 | + } |
51 | 68 |
|
52 | 69 | public Use build() { |
53 | 70 | return use; |
54 | 71 | } |
| 72 | + |
| 73 | + public static final class UseErrorsBuilder { |
| 74 | + private final UseErrors useErrors; |
| 75 | + |
| 76 | + UseErrorsBuilder() { |
| 77 | + this.useErrors = new UseErrors(); |
| 78 | + } |
| 79 | + |
| 80 | + public UseErrorsBuilder error(String name, Consumer<UseErrorBuilder> errorConsumer) { |
| 81 | + final UseErrorBuilder errorBuilder = new UseErrorBuilder(); |
| 82 | + errorConsumer.accept(errorBuilder); |
| 83 | + this.useErrors.withAdditionalProperty(name, errorBuilder.build()); |
| 84 | + return this; |
| 85 | + } |
| 86 | + |
| 87 | + public UseErrors build() { |
| 88 | + return useErrors; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + public static final class UseErrorBuilder { |
| 93 | + private final Error error; |
| 94 | + |
| 95 | + UseErrorBuilder() { |
| 96 | + this.error = new Error(); |
| 97 | + } |
| 98 | + |
| 99 | + public UseErrorBuilder type(String expression) { |
| 100 | + ErrorType errorType = new ErrorType(); |
| 101 | + try { |
| 102 | + errorType.withLiteralErrorType(new UriTemplate().withLiteralUri(new URI(expression))); |
| 103 | + } catch (URISyntaxException ex) { |
| 104 | + errorType.withExpressionErrorType(expression); |
| 105 | + } |
| 106 | + this.error.setType(errorType); |
| 107 | + return this; |
| 108 | + } |
| 109 | + |
| 110 | + public UseErrorBuilder type(URI errorType) { |
| 111 | + this.error.setType( |
| 112 | + new ErrorType().withLiteralErrorType(new UriTemplate().withLiteralUri(errorType))); |
| 113 | + return this; |
| 114 | + } |
| 115 | + |
| 116 | + public UseErrorBuilder status(int status) { |
| 117 | + this.error.setStatus(status); |
| 118 | + return this; |
| 119 | + } |
| 120 | + |
| 121 | + public UseErrorBuilder title(String expression) { |
| 122 | + this.error.setTitle(new ErrorTitle().withExpressionErrorTitle(expression)); |
| 123 | + return this; |
| 124 | + } |
| 125 | + |
| 126 | + public UseErrorBuilder detail(String expression) { |
| 127 | + this.error.setDetail(new ErrorDetails().withExpressionErrorDetails(expression)); |
| 128 | + return this; |
| 129 | + } |
| 130 | + |
| 131 | + public Error build() { |
| 132 | + return error; |
| 133 | + } |
| 134 | + } |
55 | 135 | } |
0 commit comments