Errors
Description
Section titled “Description”A call that fails returns an error in Rust and throws one in JavaScript, Python, Swift and Kotlin. Every error has a class, which says what went wrong, a message, and the fields of its class, under the same names in every language.
Rust returns the error type of the call, such as RendererError from Renderer::render, and each class below names the variants it covers. A variant with fields holds them by name, as RendererError::DeviceError { kind, label, detail } does. The other languages raise the class itself:
- JavaScript throws an
Errorwhosenameis the class, such asDeviceError, with each field as a property. - Python raises the class, with each field as an attribute. Every class is a
FragmentColorError, soexcept FragmentColorErrorcatches them all. A few are also a builtin exception, as their entries below say. - Swift throws
FragmentColorError, an enum with a case for each class. A case takes the class name without itsErrorsuffix, such as.Device, and.Otherstands for the base class. Each case holds themessageand the fields, andnamereturns the class name. - Kotlin throws
FragmentColorException, with a subclass for each class under Swift’s case names, such asFragmentColorException.Device. Each one holds themessageand the fields, andnamereturns the class name.
The message is the same text everywhere: Error.message in JavaScript, str(error) in Python, message, localizedDescription and description in Swift, and message and toString() in Kotlin. Each field holds the same value in every language. In JavaScript ErrorKind is a numeric enum, and a kind is the number to compare with ErrorKind.Memory and the other values.
An argument of the wrong type, such as a string where a number goes, is a TypeError in JavaScript and Python. Swift refuses it when the code compiles, and so does Kotlin, except Model(mesh, material), which throws IllegalArgumentException for an object that is no material.
Classes
Section titled “Classes”FragmentColorError
Section titled “FragmentColorError”The base class, for an error with no class of its own: WGSL that fails to parse, a uniform the shader doesn’t declare, a vertex the shader can’t read, a target from another Renderer, a window whose handle a target can’t get, or a GPU that fails to open. In Rust that is every ShaderError, MeshError and DisplayError, and each variant of RendererError, PassError, TextureError and InitializationError that no other class names. Swift throws it as .Other, and Kotlin as FragmentColorException.Other.
InvalidInputError
Section titled “InvalidInputError”A value the call can’t take: a size with a side of zero or past the device’s limits, a color it can’t read, a viewport with no area, texture bytes that don’t match their size, or a number that isn’t finite. In Rust that is every SizeError, ColorError, ScreenRegionError and ModelError, TextureError::InvalidInput and TextureError::Region, the Color, Viewport, ViewportLimit, InvalidColorTarget and DispatchLimit variants of PassError, and LightError::NotFinite and LightError::ZeroDirection.
JavaScript, Python, Swift and Kotlin also raise it before the call runs, for NaN, an infinity or a number past the 32-bit float range where a float goes. JavaScript raises it for a count that isn’t a whole number in its type’s range, and Python for an int count below 0 or past that range.
UnsupportedMipmapFormatError
Section titled “UnsupportedMipmapFormatError”A texture format that the CPU mipmap builder can’t fill, for a texture created with mipmaps. In Rust it is TextureError::UnsupportedMipmapFormat.
MalformedImageError
Section titled “MalformedImageError”Image bytes that can’t be decoded, such as a truncated PNG. In Rust it is TextureError::MalformedImageError.
OccludedSurfaceError
Section titled “OccludedSurfaceError”A window that is hidden or minimized, which leaves render no frame to draw on. Skip that frame and draw the next one. In Rust it is RendererError::SurfaceError(SurfaceError::Occluded).
DeviceError
Section titled “DeviceError”A GPU error that an error scope caught. Renderer::render returns one when a pipeline fails to build or a frame’s commands fail, in place of drawing nothing without a word. In Rust it is RendererError::DeviceError, and on the web a readback, such as get_image, returns TextureError::DeviceError with the same fields.
It carries three fields:
kind, anErrorKind, the kind of error the scope caught:Validation: a call broke a rule of the GPU API, such as a bind group that doesn’t match its layout.Internal: the GPU or its driver failed for a reason of its own, such as a shader the backend compiler refuses.Memory: the device ran out of memory.
label, the step that raised it:render pipeline,compute pipeline,light binning pipelineorframe, and in Rust, Python, Swift and Kotlin also a bind group or texture view inside the frame.detail, the device’s own words about what went wrong.
Running out of memory or an internal failure leaves an object invalid, and each later use of that object fails validation. So when one error causes others, the error that returns is the first in the order memory, internal, validation. Rust writes the kind as ErrorKind::Memory, JavaScript and Python as ErrorKind.Memory, Swift as ErrorKind.memory and Kotlin as ErrorKind.MEMORY.
The message carries all three, as in Device error (validation) in 'frame': .... Swift’s case is .Device(message:kind:label:detail:).
On the web the GPU reports an error after render has returned, so the next call throws it, as Renderer::render describes.
LimitError
Section titled “LimitError”Something in a shader or mesh past one of the device’s limits, such as a bind group index past max_bind_groups or a vertex stride past max_vertex_buffer_array_stride. Renderer::render checks for it before it takes a frame. In Rust it is RendererError::LimitError, with four fields: item, what passes the limit, limit, the limit’s name, value, what the item needs, and max, the device’s maximum. The Shader page lists the checks.
FeatureError
Section titled “FeatureError”Something in a shader that needs a feature the device lacks, such as texture atomics or f16. In Rust it is RendererError::FeatureError, with two fields: item, what needs the feature, and feature, the wgpu flag the device lacks, such as SHADER_F16.
BindingError
Section titled “BindingError”A texture uniform that was never set, or whose texture its slot can’t take by format, usage, dimension, sample count or mip levels. In Rust it is RendererError::BindingError, with three fields: uniform, the uniform’s name, expected, what the slot takes, and found, what it got.
EntryError
Section titled “EntryError”A render shader without the @vertex or @fragment entry point its pass runs. In Rust it is RendererError::EntryError, with one field, stage, the entry point it lacks.
SceneLoadError
Section titled “SceneLoadError”A scene file that fails to load, such as a glTF file the parser refuses or one whose data FragmentColor can’t use. In Rust it is every SceneLoadError. In Python it is also a RuntimeError.
LightError
Section titled “LightError”A light setter that doesn’t fit the light’s kind, such as a direction on a point light, or a range below zero. In Rust it is LightError::FieldNotApplicable and LightError::NegativeRange. In Python it is also a ValueError.
CameraError
Section titled “CameraError”Camera parameters that leave no usable view, such as a field of view of zero or a near plane equal to the far one. In Rust it is every CameraError. In Python it is also a ValueError.
MaterialError
Section titled “MaterialError”A value a material setter or map can’t use, such as UV set 2 or zero toon bands. A map’s texture input that makes no texture raises the class create_texture raises for the same input. In Rust it is every other MaterialError. In Python it is also a ValueError.
Example
Section titled “Example”use fragmentcolor::{ErrorKind, Renderer, Shader};
let renderer = Renderer::new();let target = renderer.create_texture_target([16, 16]).await?;let shader = Shader::default();
// A GPU error in this frame returns as a DeviceError.renderer.render(&shader, &target)?;
// The kind of GPU error a DeviceError carries.let kind = ErrorKind::Memory;import { ErrorKind, Renderer, Shader } from "fragmentcolor";
const renderer = new Renderer();const target = await renderer.createTextureTarget([16, 16]);const shader = Shader.default();
// A GPU error in this frame returns as a DeviceError.renderer.render(shader, target);
// The kind of GPU error a DeviceError carries.const kind = ErrorKind.Memory;from fragmentcolor import ErrorKind, Renderer, Shader
renderer = Renderer()target = renderer.create_texture_target([16, 16])shader = Shader.default()
# A GPU error in this frame returns as a DeviceError.renderer.render(shader, target)
# The kind of GPU error a DeviceError carries.kind = ErrorKind.Memoryimport FragmentColor
let renderer = Renderer()let target = try await renderer.createTextureTarget([16, 16])let shader = Shader.default()
// A GPU error in this frame returns as a DeviceError.try renderer.render(shader, target)
// The kind of GPU error a DeviceError carries.let kind = ErrorKind.memoryimport org.fragmentcolor.*
val renderer = Renderer()val target = renderer.createTextureTarget(listOf(16u, 16u))val shader = Shader.default()
// A GPU error in this frame returns as a DeviceError.renderer.render(shader, target)
// The kind of GPU error a DeviceError carries.val kind = ErrorKind.MEMORY