|
| 1 | +package com.fasterxml.jackson.dataformat.toml; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.core.JsonLocation; |
| 4 | +import com.fasterxml.jackson.core.JsonParser; |
| 5 | +import com.fasterxml.jackson.core.exc.StreamReadException; |
| 6 | +import com.fasterxml.jackson.core.io.ContentReference; |
| 7 | +import com.fasterxml.jackson.core.util.RequestPayload; |
| 8 | + |
| 9 | +public class JacksonTomlParseException extends StreamReadException { |
| 10 | + private JacksonTomlParseException(JsonParser p, String msg, JsonLocation loc) { |
| 11 | + super(p, msg, loc); |
| 12 | + } |
| 13 | + |
| 14 | + private JacksonTomlParseException(String msg, JsonLocation loc, Throwable rootCause) { |
| 15 | + super(msg, loc, rootCause); |
| 16 | + } |
| 17 | + |
| 18 | + @Override |
| 19 | + public JacksonTomlParseException withParser(JsonParser p) { |
| 20 | + this._processor = p; |
| 21 | + return this; |
| 22 | + } |
| 23 | + |
| 24 | + @Override |
| 25 | + public JacksonTomlParseException withRequestPayload(RequestPayload p) { |
| 26 | + this._requestPayload = p; |
| 27 | + return this; |
| 28 | + } |
| 29 | + |
| 30 | + static class ErrorContext { |
| 31 | + private final ContentReference ContentReference; |
| 32 | + private final JsonParser parser; |
| 33 | + |
| 34 | + ErrorContext(ContentReference ContentReference, JsonParser parser) { |
| 35 | + this.ContentReference = ContentReference; |
| 36 | + this.parser = parser; |
| 37 | + } |
| 38 | + |
| 39 | + ErrorBuilder atPosition(Lexer lexer) { |
| 40 | + return new ErrorBuilder(lexer); |
| 41 | + } |
| 42 | + |
| 43 | + class ErrorBuilder { |
| 44 | + private final JsonLocation location; |
| 45 | + |
| 46 | + private ErrorBuilder(Lexer lexer) { |
| 47 | + this.location = new JsonLocation( |
| 48 | + ContentReference, |
| 49 | + -1, |
| 50 | + lexer.getCharPos(), |
| 51 | + lexer.getLine() + 1, |
| 52 | + lexer.getColumn() + 1 |
| 53 | + ); |
| 54 | + } |
| 55 | + |
| 56 | + JacksonTomlParseException unexpectedToken(TomlToken actual, String expected) { |
| 57 | + return new JacksonTomlParseException( |
| 58 | + parser, |
| 59 | + "Unexpected token: Got " + actual + ", expected " + expected, |
| 60 | + location |
| 61 | + ); |
| 62 | + } |
| 63 | + |
| 64 | + JacksonTomlParseException generic(String message) { |
| 65 | + return new JacksonTomlParseException(parser, message, location); |
| 66 | + } |
| 67 | + |
| 68 | + JacksonTomlParseException outOfBounds(NumberFormatException cause) { |
| 69 | + JacksonTomlParseException parseException = new JacksonTomlParseException("Number out of bounds", location, cause); |
| 70 | + parseException._processor = parser; |
| 71 | + return parseException; |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +} |
0 commit comments