diff --git a/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SClosedChannelException.java b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SClosedChannelException.java new file mode 100644 index 0000000..6a1e3c0 --- /dev/null +++ b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SClosedChannelException.java @@ -0,0 +1,168 @@ +/* + * Copyright © 2024 Mark Raynsford https://www.io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +package com.io7m.seltzer.io; + +import java.util.Map; +import java.util.Optional; + +/** + * A convenient extension of the standard {@link java.nio.channels.ClosedChannelException} class. + */ + +public class SClosedChannelException extends SIOException +{ + /** + * Construct an exception. + * + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final Throwable cause, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(cause, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param cause The cause + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final Throwable cause, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(cause, inErrorCode, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + */ + + public SClosedChannelException( + final String message, + final Throwable cause, + final String inErrorCode) + { + super(message, cause, inErrorCode); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + */ + + public SClosedChannelException( + final String message, + final Throwable cause, + final String inErrorCode, + final Map inAttributes) + { + super(message, cause, inErrorCode, inAttributes); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final String message, + final Throwable cause, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(message, cause, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final String message, + final Throwable cause, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(message, cause, inErrorCode, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final String message, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(message, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SClosedChannelException( + final String message, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(message, inErrorCode, inRemediatingAction); + } +} diff --git a/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SEOFException.java b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SEOFException.java new file mode 100644 index 0000000..7f8fd1b --- /dev/null +++ b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SEOFException.java @@ -0,0 +1,168 @@ +/* + * Copyright © 2024 Mark Raynsford https://www.io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +package com.io7m.seltzer.io; + +import java.util.Map; +import java.util.Optional; + +/** + * A convenient extension of the standard {@link java.io.EOFException} class. + */ + +public class SEOFException extends SIOException +{ + /** + * Construct an exception. + * + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final Throwable cause, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(cause, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param cause The cause + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final Throwable cause, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(cause, inErrorCode, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + */ + + public SEOFException( + final String message, + final Throwable cause, + final String inErrorCode) + { + super(message, cause, inErrorCode); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + */ + + public SEOFException( + final String message, + final Throwable cause, + final String inErrorCode, + final Map inAttributes) + { + super(message, cause, inErrorCode, inAttributes); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final String message, + final Throwable cause, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(message, cause, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param cause The cause + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final String message, + final Throwable cause, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(message, cause, inErrorCode, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param inErrorCode The error code + * @param inAttributes The attributes + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final String message, + final String inErrorCode, + final Map inAttributes, + final Optional inRemediatingAction) + { + super(message, inErrorCode, inAttributes, inRemediatingAction); + } + + /** + * Construct an exception. + * + * @param message The message + * @param inErrorCode The error code + * @param inRemediatingAction The remediating action + */ + + public SEOFException( + final String message, + final String inErrorCode, + final Optional inRemediatingAction) + { + super(message, inErrorCode, inRemediatingAction); + } +} diff --git a/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SIOException.java b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SIOException.java index 653c007..cf035c5 100644 --- a/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SIOException.java +++ b/com.io7m.seltzer.io/src/main/java/com/io7m/seltzer/io/SIOException.java @@ -28,7 +28,7 @@ * A convenient extension of the standard {@link IOException} class. */ -public final class SIOException +public class SIOException extends IOException implements SStructuredErrorExceptionType { @@ -207,25 +207,25 @@ public SIOException( } @Override - public String errorCode() + public final String errorCode() { return this.errorCode; } @Override - public Map attributes() + public final Map attributes() { return this.attributes; } @Override - public Optional remediatingAction() + public final Optional remediatingAction() { return this.remediatingAction; } @Override - public Optional exception() + public final Optional exception() { return Optional.of(this); } diff --git a/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SClosedChannelExceptionTest.java b/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SClosedChannelExceptionTest.java new file mode 100644 index 0000000..9263397 --- /dev/null +++ b/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SClosedChannelExceptionTest.java @@ -0,0 +1,284 @@ +/* + * Copyright © 2023 Mark Raynsford https://www.io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +package com.io7m.seltzer.tests; + +import com.io7m.seltzer.io.SClosedChannelException; +import net.jqwik.api.Arbitraries; +import net.jqwik.api.Arbitrary; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import net.jqwik.api.Provide; + +import java.io.IOException; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Structured error tests. + */ + +public final class SClosedChannelExceptionTest +{ + @Provide + public Arbitrary throwables() + { + return Arbitraries.strings() + .map(IOException::new); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param action An action + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + message1, + exception, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + message1, + exception, + errorCode + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.empty(), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException2( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + message1, + exception, + errorCode, + attributes1 + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.empty(), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param attributes1 A set of attributes + */ + + @Property + public void testException3( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll Map attributes1) + { + final var sioException = + new SClosedChannelException( + message1, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.of(action), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param action An action + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String action, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + exception, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(exception.getMessage(), sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param action An action + * @param exception An exception + */ + + @Property + public void testException4( + final @ForAll String errorCode, + final @ForAll String action, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + exception, + errorCode, + Optional.of(action) + ); + + assertEquals(exception.getMessage(), sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(Map.of(), sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param message1 The message + * @param errorCode The error code + * @param action An action + */ + + @Property + public void testException5( + final @ForAll String message1, + final @ForAll String errorCode, + final @ForAll String action) + { + final var sioException = + new SClosedChannelException( + message1, + errorCode, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param action An action + * @param exception An exception + */ + + @Property + public void testException6( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SClosedChannelException( + message1, + exception, + errorCode, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(Map.of(), sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } +} diff --git a/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SEOFExceptionTest.java b/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SEOFExceptionTest.java new file mode 100644 index 0000000..785ad08 --- /dev/null +++ b/com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SEOFExceptionTest.java @@ -0,0 +1,284 @@ +/* + * Copyright © 2023 Mark Raynsford https://www.io7m.com + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR + * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + + +package com.io7m.seltzer.tests; + +import com.io7m.seltzer.io.SEOFException; +import net.jqwik.api.Arbitraries; +import net.jqwik.api.Arbitrary; +import net.jqwik.api.ForAll; +import net.jqwik.api.Property; +import net.jqwik.api.Provide; + +import java.io.IOException; +import java.util.Map; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Structured error tests. + */ + +public final class SEOFExceptionTest +{ + @Provide + public Arbitrary throwables() + { + return Arbitraries.strings() + .map(IOException::new); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param action An action + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + message1, + exception, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + message1, + exception, + errorCode + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.empty(), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException2( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + message1, + exception, + errorCode, + attributes1 + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.empty(), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param attributes1 A set of attributes + */ + + @Property + public void testException3( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll Map attributes1) + { + final var sioException = + new SEOFException( + message1, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(Optional.of(action), sioException.remediatingAction()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param action An action + * @param attributes1 A set of attributes + * @param exception An exception + */ + + @Property + public void testException0( + final @ForAll String errorCode, + final @ForAll String action, + final @ForAll Map attributes1, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + exception, + errorCode, + attributes1, + Optional.of(action) + ); + + assertEquals(exception.getMessage(), sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(attributes1, sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param action An action + * @param exception An exception + */ + + @Property + public void testException4( + final @ForAll String errorCode, + final @ForAll String action, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + exception, + errorCode, + Optional.of(action) + ); + + assertEquals(exception.getMessage(), sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(Map.of(), sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param message1 The message + * @param errorCode The error code + * @param action An action + */ + + @Property + public void testException5( + final @ForAll String message1, + final @ForAll String errorCode, + final @ForAll String action) + { + final var sioException = + new SEOFException( + message1, + errorCode, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } + + /** + * Exception fields work. + * + * @param errorCode The error code + * @param message1 A message + * @param action An action + * @param exception An exception + */ + + @Property + public void testException6( + final @ForAll String errorCode, + final @ForAll String message1, + final @ForAll String action, + final @ForAll("throwables") Throwable exception) + { + final var sioException = + new SEOFException( + message1, + exception, + errorCode, + Optional.of(action) + ); + + assertEquals(message1, sioException.message()); + assertEquals(errorCode, sioException.errorCode()); + assertEquals(Map.of(), sioException.attributes()); + assertEquals(sioException, sioException.exception().orElseThrow()); + assertEquals(action, sioException.remediatingAction().orElseThrow()); + } +}