001
002package io.vrap.rmf.base.client.http;
003
004import java.util.Arrays;
005import java.util.Optional;
006
007import io.vrap.rmf.base.client.ResponseSerializer;
008import io.vrap.rmf.base.client.error.HttpExceptionFactory;
009
010/**
011 * Used to convert API errors to Exceptions. Uses the {@link HttpExceptionFactory} to convert responses to Exceptions
012 */
013public interface ErrorMiddleware extends Middleware {
014
015    enum ExceptionMode {
016        COMPLETION_EXCEPTION("completion_exception"), UNWRAP_COMPLETION_EXCEPTION("unwrap");
017        private final String mode;
018
019        ExceptionMode(String mode) {
020            this.mode = mode;
021        }
022
023        public String getMode() {
024            return mode;
025        }
026
027        public static Optional<ExceptionMode> get(String mode) {
028            return Arrays.stream(ExceptionMode.values()).filter(env -> env.mode.equals(mode)).findFirst();
029        }
030    }
031
032    static ErrorMiddleware of() {
033        return of(HttpExceptionFactory.of(ResponseSerializer.of()), ExceptionMode.COMPLETION_EXCEPTION);
034    }
035
036    @Deprecated
037    static ErrorMiddleware of(final ResponseSerializer serializer) {
038        return of(HttpExceptionFactory.of(serializer), ExceptionMode.COMPLETION_EXCEPTION);
039    }
040
041    static ErrorMiddleware of(final HttpExceptionFactory exceptionFactory) {
042        return of(exceptionFactory, ExceptionMode.COMPLETION_EXCEPTION);
043    }
044
045    static ErrorMiddleware of(final ExceptionMode exceptionMode) {
046        return of(HttpExceptionFactory.of(ResponseSerializer.of()), exceptionMode);
047    }
048
049    static ErrorMiddleware of(final HttpExceptionFactory exceptionFactory, final ExceptionMode exceptionMode) {
050        return new ErrorMiddlewareImpl(exceptionFactory, exceptionMode);
051    }
052}