001package gudusoft.gsqlparser.pp2;
002
003import gudusoft.gsqlparser.TSyntaxError;
004
005import java.util.ArrayList;
006import java.util.Collections;
007import java.util.List;
008
009/**
010 * Thrown by the pp2 engine when {@link Pp2FormatOptions#tolerantMode} is
011 * {@code false} and parsing fails for one or more regions.
012 *
013 * <p>In tolerant mode (the default) parse failures are recovered by the
014 * lexical fallback renderer and surfaced as
015 * {@link FormatDiagnostic FormatDiagnostics} on the result, not as
016 * exceptions. Strict callers (e.g., CI lint that must reject malformed
017 * SQL outright) opt out by flipping {@code tolerantMode = false}; they then
018 * see this exception with the originating parser diagnostics attached.
019 *
020 * <p>Note: {@code FormatDiagnostic} is introduced in slice S3. Until S3 lands
021 * this exception only carries the underlying {@link TSyntaxError parser
022 * diagnostics}; the result-shaped diagnostics will be wired in alongside
023 * {@code Pp2FormatResult}.
024 */
025public class Pp2ParseException extends RuntimeException {
026
027    private static final long serialVersionUID = 1L;
028
029    private final List<TSyntaxError> parserErrors;
030
031    public Pp2ParseException(String message) {
032        super(message);
033        this.parserErrors = Collections.emptyList();
034    }
035
036    public Pp2ParseException(String message, List<TSyntaxError> parserErrors) {
037        super(message);
038        this.parserErrors = parserErrors == null
039            ? Collections.<TSyntaxError>emptyList()
040            : Collections.unmodifiableList(new ArrayList<TSyntaxError>(parserErrors));
041    }
042
043    public Pp2ParseException(String message, Throwable cause) {
044        super(message, cause);
045        this.parserErrors = Collections.emptyList();
046    }
047
048    /** Immutable view of the parser diagnostics that triggered this failure. */
049    public List<TSyntaxError> getParserErrors() {
050        return parserErrors;
051    }
052}