Skip to content

Commit

Permalink
improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hohwille committed Nov 1, 2023
1 parent fcbbb5c commit 68d567c
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 35 deletions.
5 changes: 5 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,10 @@
<groupId>${project.groupId}</groupId>
<artifactId>mmm-base</artifactId>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>mmm-scanner</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,7 @@ public <E extends Enum<E>> E readValueAsEnum(Class<E> enumType) {
String stringValue = readValueAsString();
E e = mapping.fromString(stringValue);
if ((e == null) && (stringValue != null)) {
throw new IllegalArgumentException(
"The string value '" + stringValue + "' is not an enum of type " + enumType.getName());
throw error("The string value '" + stringValue + "' is not an enum of type " + enumType.getName());
}
return e;
} else {
Expand All @@ -193,8 +192,7 @@ public <E extends Enum<E>> E readValueAsEnum(Class<E> enumType) {
}
E e = mapping.fromOrdinal(ordinalValue);
if ((e == null) && (ordinalValue != null)) {
throw new IllegalArgumentException(
"The integer value '" + ordinalValue + "' is not an ordinal of enum type " + enumType.getName());
throw error("The integer value '" + ordinalValue + "' is not an ordinal of enum type " + enumType.getName());
}
return e;
}
Expand Down Expand Up @@ -415,7 +413,7 @@ public Object readValue(boolean recursive) {
value = map;
} else {
expect(State.VALUE, State.START_ARRAY, State.START_OBJECT);
throw new IllegalStateException();
throw errorIllegalState();
}
}
return value;
Expand Down Expand Up @@ -503,13 +501,21 @@ public void skipValue() {
case NAME:
break;
case DONE:
throw new IllegalStateException();
errorIllegalState();
}
next();
}
}
}

/**
* @return nothing, just to ensure exit you may throw result of this method.
*/
protected RuntimeException errorIllegalState() {

return error("Illegal state");
}

/**
* @param message the error message.
* @return nothing, just to ensure exit you may throw result of this method.
Expand All @@ -526,10 +532,19 @@ protected RuntimeException error(String message) {
*/
protected RuntimeException error(String message, Throwable cause) {

if (this.name != null) {
message = message + "(at property '" + this.name + "')";
throw new IllegalStateException(appendContextDetails(message), cause);
}

/**
* @param message the message to report (e.g. error or warning).
* @return the given {@code message} with contextual details appended (e.g. the line number in the content to parse).
*/
protected String appendContextDetails(String message) {

if (this.name == null) {
return message;
}
throw new IllegalStateException(message, cause);
return message + "(at property '" + this.name + "')";
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package io.github.mmm.marshall;

import io.github.mmm.scanner.CharStreamScanner;

/**
* {@link AbstractStructuredReader} that {@link #readValue() reads values} as {@link Object}.
*/
@SuppressWarnings("exports")
public abstract class AbstractStructuredScannerReader extends AbstractStructuredValueReader {

/** The {@link CharStreamScanner} to read from. */
protected final CharStreamScanner reader;

/**
* The constructor.
*
* @param scanner the {@link CharStreamScanner} to read from.
* @param format the {@link #getFormat() format}.
*/
public AbstractStructuredScannerReader(CharStreamScanner scanner, StructuredFormat format) {

super(format);
this.reader = scanner;
}

@Override
protected String appendContextDetails(String message) {

StringBuilder sb = new StringBuilder(message);
sb.append('(');
if (this.name != null) {
sb.append("at property '");
sb.append(this.name);
sb.append("' ");
}
sb.append("in line ");
sb.append(this.reader.getLine());
sb.append(" and column ");
sb.append(this.reader.getColumn());
sb.append(')');
return sb.toString();
}

@Override
public void close() {

this.reader.close();
}

}
2 changes: 2 additions & 0 deletions core/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

requires transitive io.github.mmm.base;

requires static io.github.mmm.scanner;

exports io.github.mmm.marshall;

exports io.github.mmm.marshall.size;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import java.io.Reader;

import io.github.mmm.base.filter.CharFilter;
import io.github.mmm.marshall.AbstractStructuredValueReader;
import io.github.mmm.marshall.AbstractStructuredScannerReader;
import io.github.mmm.marshall.MarshallingConfig;
import io.github.mmm.marshall.StructuredFormat;
import io.github.mmm.marshall.StructuredReader;
Expand All @@ -19,7 +19,7 @@
*
* @since 1.0.0
*/
public class JsonReader extends AbstractStructuredValueReader {
public class JsonReader extends AbstractStructuredScannerReader {

private static final CharFilter NUMBER_START_FILTER = c -> ((c >= '0') && (c <= '9')) || (c == '+') || (c == '-')
|| (c == '.');
Expand All @@ -29,8 +29,6 @@ public class JsonReader extends AbstractStructuredValueReader {

private static final CharFilter SPACE_FILTER = c -> (c == ' ') || (c == '\t') || (c == '\n') || (c == '\r');

private CharStreamScanner reader;

private boolean requireQuotedProperties;

private JsonState jsonState;
Expand All @@ -45,13 +43,12 @@ public class JsonReader extends AbstractStructuredValueReader {
/**
* The constructor.
*
* @param reader the {@link Reader} with the JSON to parse.
* @param scanner the {@link Reader} with the JSON to parse.
* @param format the {@link #getFormat() format}.
*/
public JsonReader(CharStreamScanner reader, StructuredFormat format) {
public JsonReader(CharStreamScanner scanner, StructuredFormat format) {

super(format);
this.reader = reader;
super(scanner, format);
Boolean unquotedProperties = format.getConfig().get(MarshallingConfig.OPT_UNQUOTED_PROPERTIES);
this.requireQuotedProperties = Boolean.FALSE.equals(unquotedProperties);
this.jsonState = new JsonState();
Expand Down Expand Up @@ -91,8 +88,7 @@ public State next() {
propertyName = this.reader.readUntil('"', false, '\\');
} else {
if (this.requireQuotedProperties) {
throw new IllegalStateException(
"Expected quoted property but found character " + c + " (0x" + Integer.toHexString(c) + ").");
error("Expected quoted property but found character " + c + " (0x" + Integer.toHexString(c) + ").");
}
propertyName = this.reader.readUntil(':', false);
if (propertyName == null) {
Expand Down Expand Up @@ -134,7 +130,7 @@ private void nextValue(char c) {
if (b != null) {
nextValue(b);
} else {
throw new IllegalStateException("Unexpected JSON character '" + c + "'");
error("Unexpected JSON character '" + c + "'");
}
}
}
Expand Down Expand Up @@ -178,7 +174,7 @@ private void nextNumber() {
}
nextValue(number);
} catch (NumberFormatException e) {
throw new IllegalArgumentException("Invalid number: " + numberString);
error("Invalid number: " + numberString, e);
}
}

Expand Down Expand Up @@ -234,11 +230,8 @@ public void close() {
if (this.jsonState == null) {
return;
}
// if (this.readerState.parent != null) {
// throw new IllegalStateException("Not at end!");
// }
super.close();
this.jsonState = null;
this.reader = null;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import io.github.mmm.base.filter.CharFilter;
import io.github.mmm.base.filter.ListCharFilter;
import io.github.mmm.marshall.AbstractStructuredValueReader;
import io.github.mmm.marshall.AbstractStructuredScannerReader;
import io.github.mmm.marshall.StructuredFormat;
import io.github.mmm.marshall.StructuredReader;
import io.github.mmm.marshall.spi.StructuredNodeType;
Expand All @@ -19,16 +19,14 @@
*
* @since 1.0.0
*/
public class YamlReader extends AbstractStructuredValueReader {
public class YamlReader extends AbstractStructuredScannerReader {

private static final CharFilter NOT_NEWLINE_FILTER = CharFilter.NEWLINE.negate();

private static final CharFilter SPACE_FILTER = c -> (c == ' ') || (c == '\t');

private static final CharFilter VALUE_FILTER = new ListCharFilter(':', ',', '{', '[', '&', '\n', '\r');

private CharStreamScanner reader;

private YamlState yamlState;

private YamlExpectedType expectedState;
Expand Down Expand Up @@ -60,8 +58,7 @@ public class YamlReader extends AbstractStructuredValueReader {
*/
public YamlReader(CharStreamScanner reader, StructuredFormat format) {

super(format);
this.reader = reader;
super(reader, format);
this.yamlState = new YamlState();
this.expectedState = YamlExpectedType.ANY_VALUE;
this.nextColumn = -1;
Expand Down Expand Up @@ -439,11 +436,8 @@ public void close() {
if (this.yamlState == null) {
return;
}
// if (this.readerState.parent != null) {
// throw new IllegalStateException("Not at end!");
// }
this.yamlState = null;
this.reader = null;
super.close();
}

@Override
Expand Down

0 comments on commit 68d567c

Please sign in to comment.