-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated to lombrok, completed CorbadoServerException, Completed
ValidationService integration test
- Loading branch information
1 parent
ee55af1
commit 6bed96b
Showing
9 changed files
with
298 additions
and
180 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 121 additions & 10 deletions
131
src/main/java/com/corbado/exceptions/CorbadoServerException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,147 @@ | ||
package com.corbado.exceptions; | ||
|
||
import com.corbado.generated.invoker.ApiException; | ||
import com.google.gson.Gson; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import lombok.AllArgsConstructor; | ||
import lombok.Data; | ||
import lombok.Getter; | ||
import lombok.NoArgsConstructor; | ||
import lombok.NonNull; | ||
import lombok.ToString; | ||
|
||
/** Custom exception class for server-related errors. */ | ||
// TODO: Complete | ||
/** Custom exception class for server-related errors. */ | ||
@ToString | ||
public class CorbadoServerException extends Exception { | ||
|
||
/** The Constant serialVersionUID. */ | ||
private static final long serialVersionUID = 5970919574670247150L; | ||
|
||
private final int httpStatusCode; | ||
private final String body; | ||
/** The http status code. */ | ||
@Getter private final int httpStatusCode; | ||
|
||
/** The body. */ | ||
@NonNull @Getter private final ErrorResponse errorResponse; | ||
|
||
/** | ||
* Convert ApiException to ServerException. | ||
* | ||
* @param e ApiException to be converted | ||
* @throws StandardException If response body is not a string | ||
*/ | ||
public CorbadoServerException(final ApiException e) { | ||
public CorbadoServerException(@NonNull final ApiException e) { | ||
this(e.getCode(), e.getResponseBody()); | ||
} | ||
|
||
public CorbadoServerException(final int statusCode, final String body) { | ||
/** | ||
* Instantiates a new corbado server exception. | ||
* | ||
* @param statusCode the status code | ||
* @param body the body | ||
*/ | ||
public CorbadoServerException(final int statusCode, @NonNull final String body) { | ||
final Gson gson = new Gson(); | ||
httpStatusCode = statusCode; | ||
this.body = body; | ||
this.errorResponse = gson.fromJson(body, ErrorResponse.class); | ||
} | ||
|
||
public String getBody() { | ||
return body; | ||
/** | ||
* Gets the request id. | ||
* | ||
* @return the request id | ||
*/ | ||
public String getRequestId() { | ||
final Optional<String> requestId = | ||
Optional.ofNullable(this.errorResponse) | ||
.map(ErrorResponse::getRequestData) | ||
.map(RequestData::getRequestID); | ||
|
||
if (requestId.isPresent()) { | ||
return requestId.get(); | ||
} else { | ||
// requestId should always be present | ||
return null; | ||
} | ||
} | ||
|
||
/** | ||
* Gets the validation messages. | ||
* | ||
* @return the validation messages | ||
*/ | ||
public List<ValidationMessage> getValidationMessages() { | ||
final Optional<List<ValidationMessage>> validationMessages = | ||
Optional.ofNullable(this.errorResponse) | ||
.map(ErrorResponse::getError) | ||
.map(ErrorDetails::getValidation); | ||
|
||
if (validationMessages.isPresent()) { | ||
return validationMessages.get(); | ||
} else { | ||
return Collections.emptyList(); | ||
} | ||
} | ||
|
||
public int getHttpStatusCode() { | ||
return httpStatusCode; | ||
/** The Class ErrorResponse. */ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class ErrorResponse { | ||
|
||
/** The error. */ | ||
private ErrorDetails error; | ||
|
||
/** The http status code. */ | ||
private int httpStatusCode; | ||
|
||
/** The request data. */ | ||
private RequestData requestData; | ||
|
||
/** The runtime. */ | ||
private double runtime; | ||
} | ||
|
||
/** The Class ErrorDetails. */ | ||
@Data | ||
@AllArgsConstructor | ||
public static class ErrorDetails { | ||
|
||
/** The links. */ | ||
private List<String> links; | ||
|
||
/** The type. */ | ||
private String type; | ||
|
||
/** The validation. */ | ||
private List<ValidationMessage> validation; | ||
} | ||
|
||
/** The Class ValidationDetails. */ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class ValidationMessage { | ||
|
||
/** The field. */ | ||
private String field; | ||
|
||
/** The message. */ | ||
private String message; | ||
} | ||
|
||
/** The Class RequestData. */ | ||
@Data | ||
@AllArgsConstructor | ||
@NoArgsConstructor | ||
public static class RequestData { | ||
|
||
/** The link. */ | ||
private String link; | ||
|
||
/** The request ID. */ | ||
private String requestID; | ||
} | ||
} |
Oops, something went wrong.