Error handling
raylib-rs uses structured error types for all fallible operations. The safe
crate defines a rich set of typed errors in
raylib::core::error —
each domain (images, textures, fonts, audio, models, etc.) has its own error
enum. A top-level
RaylibError
aggregates all of them via #[from] conversions for callers that don’t need to
distinguish the source.
Drawing operations are infallible — there is no error path for
draw_rectangle, draw_text, etc. Errors arise only at resource load time.
API surface
InvalidImageError— returned byImage::load_image,Image::load_image_from_mem, etc. Variants includeNullDataFromFile(file not found / unsupported format),ZeroWidth/ZeroHeight,UnsupportedFormat.LoadTextureError— returned byload_texture,load_texture_from_image,load_render_texture.LoadFontError— returned byload_font,load_font_ex.LoadSoundError— returned byRaylibAudio::new_sound,new_wave,new_music.LoadModelError— returned byload_model,load_model_from_mesh.RaylibError— top-level enum aggregating all domain errors viaFromimpls; useful when propagating with?through a function that loads multiple resource types.raylib::init().build()panics on a second call (window context is process-global). All other APIs areResult-based.
Example
extern crate raylib;
use raylib::core::texture::Image;
use raylib::core::error::InvalidImageError;
fn main() {
match Image::load_image("nonexistent.png") {
Ok(img) => {
println!("loaded {}x{} image", img.width(), img.height());
}
Err(InvalidImageError::NullDataFromFile) => {
eprintln!("file not found or unsupported format");
}
Err(e) => {
eprintln!("unexpected image error: {e}");
}
}
}
Gotchas
- Error variants are typed, not stringly-typed.
Unlike some thin C-binding layers, raylib-rs maps sentinel return values
to named enum variants. This makes
matchexhaustive and lets you distinguish “file not found” from “GPU upload failed” without parsing a message string. - Errors use
thiserror. Every error type implementsstd::error::ErrorandDisplay. They compose naturally withanyhow,eyre, or anyBox<dyn Error>handler. ?propagation works if you useRaylibErroras your function’s error type — every domain error has aFromimpl into it. Alternatively, the individual domain errors can be mapped with.map_err(RaylibError::from).raylib::init().build()panics, notErr. Double-init is a programming error (not a runtime condition), so it panics rather than returningErr.
See also
- Safety — how the safe wrapper prevents UB.
- Textures and images —
ImageandTexture2Dload paths. RaylibErrordocs.rs