001/*
002 * Copyright 2026 Revetware LLC.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package com.revetsec.oauth;
018
019import com.revetsec.ErrorCategory;
020import org.jspecify.annotations.NonNull;
021import org.jspecify.annotations.Nullable;
022
023import javax.annotation.concurrent.NotThreadSafe;
024import java.io.IOException;
025
026import static java.util.Objects.requireNonNull;
027
028/**
029 * A network failure, interruption, attempt-limit hold or unavailable HTTP client.
030 *
031 * @author <a href="https://www.revetkn.com">Mark Allen</a>
032 * @since 1.0.0
033 */
034@NotThreadSafe
035public final class OAuthTransportException extends OAuthException {
036        private static final long serialVersionUID = 1L;
037
038        private OAuthTransportException(@NonNull Reason reason, @Nullable IOException cause) {
039                super(reason, reason == Reason.NETWORK_FAILURE || reason == Reason.ATTEMPT_LIMIT, cause);
040        }
041
042        @NonNull
043        static OAuthTransportException fromReason(@NonNull Reason reason, @Nullable IOException cause) {
044                requireNonNull(reason);
045                if (reason.category() != ErrorCategory.TRANSPORT && reason != Reason.HTTP_CLIENT_UNAVAILABLE)
046                        throw new IllegalArgumentException("The OAuth reason is not a transport or client-configuration reason.");
047                if (reason == Reason.HTTP_CLIENT_UNAVAILABLE && cause != null)
048                        throw new IllegalArgumentException("A configuration failure cannot retain a cause.");
049                return new OAuthTransportException(reason, cause);
050        }
051}