Skip to content

Commit

Permalink
objectionary#3238 fix EOerror violations
Browse files Browse the repository at this point in the history
  • Loading branch information
c71n93 committed Aug 6, 2024
1 parent a7d9f80 commit 25f9fe7
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 27 deletions.
70 changes: 50 additions & 20 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOerror.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,11 @@

/*
* @checkstyle PackageNameCheck (4 lines)
* @checkstyle TrailingCommentCheck (3 lines)
*/
package EOorg.EOeolang;
package EOorg.EOeolang; // NOPMD

import java.util.function.Supplier;
import org.eolang.AtVoid;
import org.eolang.Atom;
import org.eolang.Dataized;
Expand Down Expand Up @@ -55,32 +57,59 @@ public final class EOerror extends PhDefault implements Atom {
/**
* Ctor.
*/
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
public EOerror() {
this.add("message", new AtVoid("message"));
}

@Override
public Phi lambda() {
throw new ExError(this.take("message"));
}

/**
* Make a message from an exception.
* @param exp The exception
* @return Message
* Builds error message from the exception.
*
* @since 0.40
*/
public static String message(final Throwable exp) {
final StringBuilder ret = new StringBuilder(0);
if (exp.getMessage() != null) {
if (!(exp instanceof ExFailure)) {
ret.append(exp.getClass().getSimpleName()).append(": ");
}
ret.append(exp.getMessage().replace("%", "%%"));
public static final class ErrorMsg implements Supplier<String> {
/**
* The exception.
*/
private final Throwable exp;

/**
* Ctor.
*
* @param exp The exception.
*/
public ErrorMsg(final Throwable exp) {
this.exp = exp;
}
if (exp.getCause() != null) {
ret.append("; caused by ").append(EOerror.message(exp.getCause()));

@Override
public String get() {
return ErrorMsg.message(this.exp);
}
return ret.toString();
}

@Override
public Phi lambda() {
throw new ExError(this.take("message"));
/**
* Make a message from an exception.
* @param exp The exception.
* @return Message.
*/
private static String message(final Throwable exp) {
final StringBuilder ret = new StringBuilder(0);
if (exp.getMessage() != null) {
if (!(exp instanceof ExFailure)) {
ret.append(exp.getClass().getSimpleName()).append(": ");
}
ret.append(exp.getMessage().replace("%", "%%"));
}
if (exp.getCause() != null) {
ret.append("; caused by ").append(ErrorMsg.message(exp.getCause()));
}
return ret.toString();
}
}

/**
Expand Down Expand Up @@ -125,6 +154,7 @@ public Phi enclosure() {
* @return String message.
* @checkstyle IllegalCatchCheck (55 lines)
*/
@SuppressWarnings("PMD.AvoidCatchingGenericException")
private static String safeMessage(final Phi enclosure) {
String result;
if (enclosure == null) {
Expand All @@ -137,10 +167,10 @@ private static String safeMessage(final Phi enclosure) {
enclosure,
new VerboseBytesAsString(raw).get()
);
} catch (final Throwable first) {
} catch (final Exception first) {
try {
result = enclosure.toString();
} catch (final Throwable second) {
} catch (final Exception second) {
result = enclosure.getClass().toString();
}
}
Expand Down
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/org/eolang/AtSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public Phi get() {
return new PhSafe(this.origin.get());
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand All @@ -90,7 +90,7 @@ public boolean put(final Phi phi) {
return this.origin.put(phi);
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand Down
10 changes: 5 additions & 5 deletions eo-runtime/src/main/java/org/eolang/PhSafe.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ public Phi take(final String name) {
return this.origin.take(name);
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand All @@ -91,7 +91,7 @@ public boolean put(final int pos, final Phi object) {
return this.origin.put(pos, object);
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand All @@ -102,7 +102,7 @@ public boolean put(final String name, final Phi object) {
return this.origin.put(name, object);
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand All @@ -123,7 +123,7 @@ public void attach(final byte[] data) {
this.origin.attach(data);
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand All @@ -134,7 +134,7 @@ public byte[] delta() {
return this.origin.delta();
} catch (final ExFailure ex) {
throw new EOerror.ExError(
new Data.ToPhi(EOerror.message(ex))
new Data.ToPhi(new EOerror.ErrorMsg(ex).get())
);
}
}
Expand Down

0 comments on commit 25f9fe7

Please sign in to comment.