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.marshall.parser; 018 019import static org.apache.juneau.commons.utils.Shorts.*; 020import static org.apache.juneau.commons.utils.StringUtils.*; 021 022import java.lang.reflect.*; 023import org.apache.juneau.marshall.*; 024 025/** 026 * Exception that indicates invalid syntax encountered during parsing. 027 * 028 * <h2 class='section'>See Also:</h2><ul> 029 * <li class='link'><a class="doclink" href="https://juneau.apache.org/docs/topics/SerializersAndParsers">Serializers and Parsers</a> 030 031 * </ul> 032 * 033 * @serial exclude 034 */ 035@SuppressWarnings({ 036 "java:S110" // Deep inheritance inherent to the exception hierarchy 037}) 038public class ParseException extends BasicRuntimeException { 039 040 private static final long serialVersionUID = 1L; 041 042 /** 043 * Creator method. 044 * 045 * <p> 046 * If the throwable is already a {@link ParseException}, we simply return that exception as-is. 047 * If the throwable is an {@link InvocationTargetException}, we unwrap the thrown exception. 048 * Otherwise we create a new {@link ParseException}. 049 * 050 * @param e The exception being wrapped or unwrapped. 051 * <br>Can be <jk>null</jk> (a new {@link ParseException} with no cause is created). 052 * @return The specified throwable wrapped or unwrapped as a {@link ParseException} (may be the same instance if it already was one). 053 */ 054 public static ParseException create(Throwable e) { 055 if (e instanceof InvocationTargetException e2) 056 e = e2.getCause(); 057 if (e instanceof ParseException e3) 058 return e3; 059 return new ParseException(e); 060 } 061 062 private static String getMessage(ParserSession session, String msg, Object...args) { 063 if (args.length != 0) 064 msg = f(msg, args); 065 066 if (nn(session)) { 067 Position p = session.getPosition(); 068 var sb = new StringBuilder(msg); 069 070 sb.append("\n\tAt: ").append(p); 071 072 var lastLocation = session.getLastLocation(); 073 if (nn(lastLocation)) { 074 sb.append("\n\tWhile parsing into: "); 075 lastLocation.forEach((k, v) -> sb.append("\n\t\t").append(k).append(": ").append(v)); 076 } 077 078 if (session.isIncludeInputInErrors()) { 079 String lines = session.getInputAsString(); 080 if (lines == null) 081 sb.append("\n\tUse BEAN_debug setting to display content."); 082 else { 083 int numLines = session.getDebugOutputLines(); 084 int start = p.line - numLines; 085 int end = p.line + numLines; 086 sb.append("\n---start--\n").append(getNumberedLines(lines, start, end)).append("---end---"); 087 } 088 } 089 090 msg = sb.toString(); 091 } 092 return msg; 093 } 094 095 /** 096 * Constructor. 097 * 098 * @param session The parser session. 099 * <br>Can be <jk>null</jk> (the message is not augmented with position/debug info). 100 * @param causedBy The inner exception. 101 * <br>Must not be <jk>null</jk>. 102 */ 103 public ParseException(ParserSession session, Exception causedBy) { 104 super(causedBy, getMessage(session, causedBy.getMessage())); 105 } 106 107 /** 108 * Constructor. 109 * 110 * @param session The parser session. 111 * <br>Can be <jk>null</jk> (the message is not augmented with position/debug info). 112 * @param message The exception message containing {@link String#format(String, Object...) String.format}-style (<c>%s</c>) arguments. 113 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 114 */ 115 public ParseException(ParserSession session, String message, Object...args) { 116 super(getMessage(session, message, args)); 117 } 118 119 /** 120 * Constructor. 121 * 122 * @param session The parser session. 123 * <br>Can be <jk>null</jk> (the message is not augmented with position/debug info). 124 * @param causedBy The cause of this exception. 125 * <br>Can be <jk>null</jk> (no cause is set). 126 * @param message The exception message containing {@link String#format(String, Object...) String.format}-style (<c>%s</c>) arguments. 127 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 128 */ 129 public ParseException(ParserSession session, Throwable causedBy, String message, Object...args) { 130 super(causedBy, getMessage(session, message, args)); 131 } 132 133 /** 134 * Constructor. 135 * 136 * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders). 137 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 138 */ 139 public ParseException(String message, Object...args) { 140 super(message, args); 141 } 142 143 /** 144 * Constructor. 145 * 146 * @param causedBy The cause of this exception. 147 * <br>Can be <jk>null</jk> (no cause is set). 148 */ 149 public ParseException(Throwable causedBy) { 150 super(causedBy); 151 } 152 153 /** 154 * Constructor. 155 * 156 * @param causedBy The cause of this exception. 157 * <br>Can be <jk>null</jk> (no cause is set). 158 * @param message The {@link String#format(String, Object...) String.format}-style message (<c>%s</c> placeholders). 159 * @param args Optional {@link String#format(String, Object...) String.format}-style arguments. 160 */ 161 public ParseException(Throwable causedBy, String message, Object...args) { 162 super(causedBy, message, args); 163 } 164 165 /** 166 * Returns the highest-level <c>ParseException</c> in the stack trace. 167 * 168 * <p> 169 * Useful for JUnit testing of error conditions. 170 * 171 * @return The root parse exception, or this exception if there isn't one. 172 */ 173 public ParseException getRootCause() { 174 ParseException t = this; 175 while (! (t.getCause() == null || ! (t.getCause() instanceof ParseException t2))) 176 t = t2; 177 return t; 178 } 179 180 @Override /* Overridden from BasicRuntimeException */ 181 public ParseException setMessage(String message, Object...args) { 182 super.setMessage(message, args); 183 return this; 184 } 185}