Skip to content

Commit

Permalink
Make exceptions into structured errors
Browse files Browse the repository at this point in the history
Fix: #8
  • Loading branch information
io7m committed Dec 6, 2024
1 parent 54a7480 commit 0fa30ea
Show file tree
Hide file tree
Showing 17 changed files with 253 additions and 203 deletions.
5 changes: 5 additions & 0 deletions com.io7m.jcoronado.api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
<url>https://www.io7m.com/software/jcoronado</url>

<dependencies>
<dependency>
<groupId>com.io7m.seltzer</groupId>
<artifactId>com.io7m.seltzer.api</artifactId>
</dependency>

<dependency>
<groupId>com.io7m.immutables-style</groupId>
<artifactId>com.io7m.immutables-style</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,38 @@

package com.io7m.jcoronado.api;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* An exception raised by a memory allocation failure.
*/

public final class VulkanAllocationFailedException extends VulkanException
public final class VulkanAllocationFailedException
extends VulkanException
{
/**
* Construct an exception.
*
* @param message The error message
* @param message The error message
* @param size The requested size
* @param alignment The requested alignment
*/

public VulkanAllocationFailedException(
final String message)
final String message,
final long size,
final long alignment)
{
super(Objects.requireNonNull(message, "message"));
super(
Objects.requireNonNull(message, "message"),
Map.ofEntries(
Map.entry("Allocation Size", Long.toUnsignedString(size)),
Map.entry("Allocation Alignment", Long.toUnsignedString(alignment))
),
"error-vulkan-memory-allocation",
Optional.empty()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,37 @@

package com.io7m.jcoronado.api;

import java.util.Objects;
import java.util.Map;
import java.util.Optional;

/**
* An exception raised by a failing Vulkan call.
*/

public final class VulkanCallFailedException extends VulkanException
public final class VulkanCallFailedException
extends VulkanException
{
private final int code;
private final String function;

/**
* Construct an exception.
*
* @param in_code The returned error code
* @param in_function The function that failed
* @param inErrorCode The returned error code
* @param inFunction The function that failed
* @param message The error message
*/

public VulkanCallFailedException(
final int in_code,
final String in_function,
final int inErrorCode,
final String inFunction,
final String message)
{
super(Objects.requireNonNull(message, "message"));
this.code = in_code;
this.function = Objects.requireNonNull(in_function, "function");
}

@Override
public boolean equals(final Object o)
{
if (this == o) {
return true;
}
if (o == null || !Objects.equals(this.getClass(), o.getClass())) {
return false;
}
final var that = (VulkanCallFailedException) o;
return this.code == that.code && Objects.equals(
this.function,
that.function);
}

@Override
public int hashCode()
{
return Objects.hash(Integer.valueOf(this.code), this.function);
}

/**
* @return The name of the function that failed
*/

public String function()
{
return this.function;
}

/**
* @return The error code returned by the function that failed
*/

public int errorCode()
{
return this.code;
super(
message,
Map.ofEntries(
Map.entry("ErrorCode", Integer.toString(inErrorCode)),
Map.entry("Function", inFunction)
),
"error-vulkan-call",
Optional.empty()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,35 @@

package com.io7m.jcoronado.api;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* An exception raised by attempting to call a method on an object that has been closed/destroyed.
*/

public final class VulkanDestroyedException extends VulkanException
public final class VulkanDestroyedException
extends VulkanException
{
/**
* Construct an exception.
*
* @param message The error message
* @param aClass The class
*/

public VulkanDestroyedException(
final String message)
final String message,
final Class<?> aClass)
{
super(Objects.requireNonNull(message, "message"));
super(
Objects.requireNonNull(message, "message"),
Map.ofEntries(
Map.entry("Class", aClass.getName())
),
"error-vulkan-destroyed",
Optional.empty()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,97 @@

package com.io7m.jcoronado.api;

import com.io7m.seltzer.api.SStructuredErrorExceptionType;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* The base type of exceptions raised by the API.
*/

public abstract class VulkanException extends Exception
public abstract class VulkanException
extends Exception
implements SStructuredErrorExceptionType<String>
{
private final Map<String, String> attributes;
private final String errorCode;
private final Optional<String> remediatingAction;

/**
* Construct an exception.
*
* @param message The message
* @param message The message
* @param inAttributes The attributes
* @param inErrorCode The error code
* @param inRemediatingAction The remediating action
*/

public VulkanException(
final String message)
final String message,
final Map<String, String> inAttributes,
final String inErrorCode,
final Optional<String> inRemediatingAction)
{
super(Objects.requireNonNull(message, "message"));
super(message);

this.attributes =
Map.copyOf(inAttributes);
this.errorCode =
Objects.requireNonNull(inErrorCode, "errorCode");
this.remediatingAction =
Objects.requireNonNull(inRemediatingAction, "remediatingAction");
}

/**
* Construct an exception.
*
* @param message The message
* @param cause The cause
* @param message The message
* @param cause The cause
* @param inAttributes The attributes
* @param inErrorCode The error code
* @param inRemediatingAction The remediating action
*/

public VulkanException(
final String message,
final Throwable cause)
final Throwable cause,
final Map<String, String> inAttributes,
final String inErrorCode,
final Optional<String> inRemediatingAction)
{
super(
Objects.requireNonNull(message, "message"),
Objects.requireNonNull(cause, "cause"));
super(message, cause);

this.attributes =
Map.copyOf(inAttributes);
this.errorCode =
Objects.requireNonNull(inErrorCode, "errorCode");
this.remediatingAction =
Objects.requireNonNull(inRemediatingAction, "remediatingAction");
}

/**
* Construct an exception.
*
* @param cause The cause
*/
@Override
public final Map<String, String> attributes()
{
return this.attributes;
}

public VulkanException(
final Throwable cause)
@Override
public final String errorCode()
{
return this.errorCode;
}

@Override
public final Optional<String> remediatingAction()
{
return this.remediatingAction;
}

@Override
public final Optional<Throwable> exception()
{
super(Objects.requireNonNull(cause, "cause"));
return Optional.of(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@

package com.io7m.jcoronado.api;

import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
* An exception raised by attempting to use a class that is not compatible with a particular
Expand All @@ -34,6 +36,11 @@ public final class VulkanIncompatibleClassException extends VulkanException
public VulkanIncompatibleClassException(
final String message)
{
super(Objects.requireNonNull(message, "message"));
super(
Objects.requireNonNull(message, "message"),
Map.of(),
"error-vulkan-incompatible-class",
Optional.empty()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package com.io7m.jcoronado.api;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;

/**
Expand All @@ -26,30 +29,33 @@
public final class VulkanMissingRequiredExtensionsException
extends VulkanException
{
private final Set<String> missing;

/**
* Construct an exception.
*
* @param in_missing The missing extensions
* @param message The error message
* @param missing The missing extensions
* @param message The error message
*/

public VulkanMissingRequiredExtensionsException(
final Set<String> in_missing,
final Set<String> missing,
final String message)
{
super(Objects.requireNonNull(message, "message"));
this.missing =
Set.copyOf(Objects.requireNonNull(in_missing, "missing"));
super(
Objects.requireNonNull(message, "message"),
createAttributes(missing),
"error-vulkan-missing-extension",
Optional.empty()
);
}

/**
* @return The set of missing extensions
*/

public Set<String> missing()
private static Map<String, String> createAttributes(
final Iterable<String> missing)
{
return this.missing;
final var r = new HashMap<String, String>();
final var index = 0;
for (final var m : missing) {
r.put("Missing Extension (%d)".formatted(Integer.valueOf(index)), m);
}
return Map.copyOf(r);
}
}
Loading

0 comments on commit 0fa30ea

Please sign in to comment.