diff --git a/plugin/src/main/groovy/org/unify4j/model/builder/HttpStatusBuilder.java b/plugin/src/main/groovy/org/unify4j/model/builder/HttpStatusBuilder.java index fed8d9b..87c96b3 100755 --- a/plugin/src/main/groovy/org/unify4j/model/builder/HttpStatusBuilder.java +++ b/plugin/src/main/groovy/org/unify4j/model/builder/HttpStatusBuilder.java @@ -19,6 +19,10 @@ public class HttpStatusBuilder { protected static List statuses; + /** + * @param statusCode - the status code + * @return new HTTP wrapper response, class {@link HttpResponse} + */ public static HttpResponse valueOf(int statusCode) { if (Collection4j.isEmpty(statuses)) { statuses = Arrays.asList( @@ -96,6 +100,58 @@ public static HttpResponse valueOf(int statusCode) { return statuses.stream().filter(e -> e.getCode() == statusCode).findFirst().orElse(new HttpResponse(-1, "unknown", "unknown", "out of HTTP status version")); } + /** + * Checks if the given HTTP status code indicates a successful response. + *

+ * A successful response is indicated by status codes in the range 200 to 299. + * This includes statuses like 200 OK, 201 Created, 204 No Content, etc. + * + * @param code the HTTP status code to check + * @return true if the code is between 200 and 299 (inclusive), false otherwise + */ + public static boolean isSuccess(int code) { + return (200 <= code) && (code <= 299); + } + + /** + * Checks if the given HTTP status code indicates a redirection response. + *

+ * A redirection response is indicated by status codes in the range 300 to 399. + * This includes statuses like 301 Moved Permanently, 302 Found, 304 Not Modified, etc. + * + * @param code the HTTP status code to check + * @return true if the code is between 300 and 399 (inclusive), false otherwise + */ + public static boolean isRedirection(int code) { + return (300 <= code) && (code <= 399); + } + + /** + * Checks if the given HTTP status code indicates a client error response. + *

+ * A client error response is indicated by status codes in the range 400 to 499. + * This includes statuses like 400 Bad Request, 401 Unauthorized, 404 Not Found, etc. + * + * @param code the HTTP status code to check + * @return true if the code is between 400 and 499 (inclusive), false otherwise + */ + public static boolean isClientError(int code) { + return (400 <= code) && (code <= 499); + } + + /** + * Checks if the given HTTP status code indicates a server error response. + *

+ * A server error response is indicated by status codes in the range 500 to 599. + * This includes statuses like 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, etc. + * + * @param code the HTTP status code to check + * @return true if the code is between 500 and 599 (inclusive), false otherwise + */ + public static boolean isServerError(int code) { + return (500 <= code) && (code <= 599); + } + // 1xx Informational responses public static final HttpResponse CONTINUE = new HttpResponse(100, "Continue", "Informational", ""); public static final HttpResponse SWITCHING_PROTOCOLS = new HttpResponse(101, "Switching Protocols", "Informational", ""); diff --git a/plugin/src/main/groovy/org/unify4j/model/response/WrapResponse.java b/plugin/src/main/groovy/org/unify4j/model/response/WrapResponse.java index 88130ba..3567184 100644 --- a/plugin/src/main/groovy/org/unify4j/model/response/WrapResponse.java +++ b/plugin/src/main/groovy/org/unify4j/model/response/WrapResponse.java @@ -102,12 +102,14 @@ public void setPagination(PaginationResponse pagination) { @JsonIgnore public boolean isError() { - return this.errors != null; + return this.errors != null || + HttpStatusBuilder.isClientError(this.statusCode) || + HttpStatusBuilder.isServerError(this.statusCode); } @JsonIgnore public boolean isSuccess() { - return this.statusCode >= HttpStatusBuilder.OK.getCode() && this.statusCode < HttpStatusBuilder.MULTIPLE_CHOICES.getCode(); + return HttpStatusBuilder.isSuccess(this.statusCode); } public String getPath() {