package tech.greenfield.vertx.irked;

import java.util.Objects;

public class HttpError extends Exception {

	private static final long serialVersionUID = -7084405660609573926L;
	
	private int statusCode;
	private String statusText;
	
	public HttpError(int statusCode, String statusText) {
		super();
		this.statusCode = statusCode;
		this.statusText = statusText;
	}
	
	public HttpError(int statusCode, String statusText, String message) {
		super(message);
		this.statusCode = statusCode;
		this.statusText = statusText;
	}
	
	public HttpError(int statusCode, String statusText, Throwable throwable) {
		super(throwable);
		this.statusCode = statusCode;
		this.statusText = statusText;
	}
	
	public HttpError(int statusCode, String statusText, String message, Throwable throwable) {
		super(message, throwable);
		this.statusCode = statusCode;
		this.statusText = statusText;
	}
	
	public int getStatusCode() {
		return statusCode;
	}
	
	public String getStatusText() {
		return statusText;
	}
	
	/**
	 * Helper method to make it easier to throw HTTP statuses out of lambdas.
	 * Outside the lambda you should catch a {@link RuntimeException} and use
	 * {@link #unwrap()} to get the original exception
	 * @return {@link RuntimeException} wrapping this status instance
	 */
	public RuntimeException uncheckedWrap() {
		return new RuntimeException(this);
	}
	
	/**
	 * Unwrap {@link RuntimeException} wrappers around a logical exception
	 * (hopefully an instance of HttpError)
	 * @param t Throweable to unwrap
	 * @return the first non RuntimeException found
	 */
	public static Throwable unwrap(Throwable t) {
		while (t instanceof RuntimeException && Objects.nonNull(t.getCause()))
			t = t.getCause();
		return t;
	}

}

@@ -58,16 +58,80 @@
 jects;%0A%0A
+import tech.greenfield.vertx.irked.status.InternalServerError;%0A%0A
 public c
@@ -1781,11 +1781,507 @@
 n t;%0A%09%7D%0A
+%09%0A%09/**%0A%09 * Helper methods for OnFail handlers to locate a wrapped HttpError or%0A%09 * create an InternalServerError from an unexpected exception (whether it%0A%09 * is wrapped or not)%0A%09 * @param t Throwable to be analyzed%0A%09 * @return the wrapped HttpError instance or a new %7B@link InternalServerError%7D wrapping%0A%09 *   the real exception%0A%09 */%0A%09public static HttpError toHttpError(Throwable t) %7B%0A%09%09t = unwrap(t);%0A%09%09if (t instanceof HttpError)%0A%09%09%09return (HttpError)t;%0A%09%09return new InternalServerError(t);%0A%09%7D
 %0A%7D%0A

add unwrapping to HttpError helper