001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions Copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2014 ForgeRock AS.
015 */
016
017package org.forgerock.openig.filter.oauth2.client;
018
019import static org.forgerock.openig.filter.oauth2.client.OAuth2Error.newAuthorizationServerError;
020
021/**
022 * An exception that is thrown when OAuth 2.0 request fails.
023 */
024public final class OAuth2ErrorException extends Exception {
025    private static final long serialVersionUID = 1L;
026    private final OAuth2Error error;
027
028    /**
029     * Creates a new exception with the provided OAuth 2.0 error.
030     *
031     * @param error
032     *            The OAuth 2.0 error.
033     */
034    public OAuth2ErrorException(final OAuth2Error error) {
035        super(error.toString());
036        this.error = error;
037    }
038
039    /**
040     * Creates a new exception with the provided OAuth 2.0 error.
041     *
042     * @param error
043     *            The OAuth 2.0 error.
044     * @param message
045     *            The message.
046     */
047    public OAuth2ErrorException(final OAuth2Error error, final String message) {
048        super(message);
049        this.error = error;
050    }
051
052    /**
053     * Creates a new exception with the provided OAuth 2.0 error.
054     *
055     * @param error
056     *            The OAuth 2.0 error.
057     * @param message
058     *            The message.
059     * @param cause
060     *            The cause.
061     */
062    public OAuth2ErrorException(final OAuth2Error error, final String message, final Throwable cause) {
063        super(message, cause);
064        this.error = error;
065    }
066
067    /**
068     * Creates a new exception with the provided OAuth 2.0 error.
069     *
070     * @param error
071     *            The OAuth 2.0 error.
072     * @param cause
073     *            The cause.
074     */
075    public OAuth2ErrorException(final OAuth2Error error, final Throwable cause) {
076        super(error.toString(), cause);
077        this.error = error;
078    }
079
080    /**
081     * Creates a new exception with the provided OAuth 2.0 error code and
082     * optional description.
083     *
084     * @param error
085     *            The error code specifying the cause of the failure.
086     * @param errorDescription
087     *            The human-readable ASCII text providing additional
088     *            information, or {@code null}.
089     * @throws NullPointerException
090     *             If {@code error} was {@code null}.
091     */
092    public OAuth2ErrorException(final String error, final String errorDescription) {
093        this(newAuthorizationServerError(error, errorDescription));
094    }
095
096    /**
097     * Creates a new exception with the provided OAuth 2.0 error code, optional
098     * description, and cause.
099     *
100     * @param error
101     *            The error code specifying the cause of the failure.
102     * @param errorDescription
103     *            The human-readable ASCII text providing additional
104     *            information, or {@code null}.
105     * @param cause
106     *            The cause.
107     * @throws NullPointerException
108     *             If {@code error} was {@code null}.
109     */
110    public OAuth2ErrorException(final String error, final String errorDescription,
111            final Throwable cause) {
112        this(newAuthorizationServerError(error, errorDescription), cause);
113    }
114
115    /**
116     * Returns the OAuth 2.0 error represented by this exception.
117     *
118     * @return The OAuth 2.0 error represented by this exception.
119     */
120    public OAuth2Error getOAuth2Error() {
121        return error;
122    }
123
124}