001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.juneau.rest.auth.oauth.flow; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020 021import java.util.*; 022 023/** 024 * Runtime exception thrown by any OAuth flow helper when the IdP rejects the request, the HTTP round-trip 025 * fails, or the response can't be parsed. 026 * 027 * <p> 028 * Unchecked because flow acquisition is typically called from startup or service-initialization code where 029 * checked exceptions would bloat call sites. 030 * 031 * @since 10.0.0 032 */ 033public class OAuthFlowException extends RuntimeException { 034 035 private static final long serialVersionUID = 1L; 036 037 private final String errorCode; 038 039 /** 040 * Constructor. 041 * 042 * @param msg The detail message. May be {@code null}. 043 */ 044 public OAuthFlowException(String msg) { 045 super(msg); 046 errorCode = null; 047 } 048 049 /** 050 * Constructor. 051 * 052 * @param msg The detail message. May be {@code null}. 053 * @param cause The cause. May be {@code null}. 054 */ 055 public OAuthFlowException(String msg, Throwable cause) { 056 super(msg, cause); 057 errorCode = null; 058 } 059 060 /** 061 * Constructor carrying the OAuth error code returned by the IdP (RFC 6749 §5.2). 062 * 063 * @param msg The detail message. May be {@code null}. 064 * @param errorCode The OAuth {@code error} code (e.g. {@code "invalid_grant"}). May be {@code null}. 065 */ 066 public OAuthFlowException(String msg, String errorCode) { 067 super(msg); 068 this.errorCode = errorCode; 069 } 070 071 /** 072 * Returns the OAuth {@code error} code the IdP returned, if this exception represents a token-endpoint error 073 * response (RFC 6749 §5.2). 074 * 075 * @return The error code (e.g. {@code "invalid_grant"}), or {@link Optional#empty()} if not an IdP error response. 076 */ 077 public Optional<String> errorCode() { 078 return o(errorCode); 079 } 080}