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.jose;
018
019import com.revetsec.ErrorCategory;
020import org.jspecify.annotations.NonNull;
021
022import javax.annotation.concurrent.NotThreadSafe;
023
024import static java.util.Objects.requireNonNull;
025
026/**
027 * Thrown when a token or a JSON Web Key Set document cannot be parsed, or exceeds a limit.
028 * <p>
029 * Its category is {@link ErrorCategory#MALFORMED_INPUT}, and its {@link JoseException.Reason} is one of
030 * {@link JoseException.Reason#TOKEN_TOO_LARGE TOKEN_TOO_LARGE}, {@link JoseException.Reason#TOKEN_SYNTAX TOKEN_SYNTAX},
031 * {@link JoseException.Reason#HEADER HEADER}, {@link JoseException.Reason#CLAIMS CLAIMS} and
032 * {@link JoseException.Reason#KEY_SET KEY_SET}. It has no cause and is never transient.
033 *
034 * @author <a href="https://www.revetkn.com">Mark Allen</a>
035 * @since 1.0.0
036 */
037@NotThreadSafe
038public final class MalformedJoseInputException extends JoseException {
039        /**
040         * The serialized form's version.
041         */
042        private static final long serialVersionUID = 1L;
043
044        private MalformedJoseInputException(@NonNull Reason reason) {
045                super(reason);
046        }
047
048        /**
049         * Returns a new instance for {@code reason}. Only Revetsec calls this.
050         *
051         * @param reason a reason in category {@link ErrorCategory#MALFORMED_INPUT}
052         * @return a new instance
053         * @throws IllegalArgumentException if {@code reason} belongs to another category
054         */
055        @NonNull
056        static MalformedJoseInputException fromReason(@NonNull Reason reason) {
057                if (requireNonNull(reason).category() != ErrorCategory.MALFORMED_INPUT)
058                        throw new IllegalArgumentException("A malformed-input reason must be in category MALFORMED_INPUT.");
059
060                return new MalformedJoseInputException(reason);
061        }
062}