easy-gl › Error Handling

Error Handling

Exception type

easy-gl throws easygl::Exception for programmer-detectable errors such as missing required features, shader/program link failures, and initialization problems. It derives from std::exception.

#include <easygl/Exception.hpp>

try {
    device.initialize(loader);
    easygl::Program prog(vertSrc, fragSrc);
} catch (const easygl::Exception& e) {
    std::cerr << "easy-gl error: " << e.what() << '\n';
} catch (const std::exception& e) {
    std::cerr << "std error: " << e.what() << '\n';
}

When exceptions are thrown

SituationSource
Required feature not supported by the driverDevice::require(Feature)
Shader compilation failureShader::compile_from_source()
Program link failureProgram::link(), Program(vertSrc, fragSrc)
Device initialization failureDevice::initialize()

Config: controlling strictness

Pass a Config struct to Device to control validation behavior:

easygl::Config cfg;
cfg.throw_on_missing_feature = true;  // throw if require() fails
cfg.enable_debug_logging      = true;  // log GL debug messages
cfg.validate_calls            = true;  // extra validation

easygl::Device device(cfg);
device.initialize(loader);

Shader and program info logs

When compilation or link fails, the info log is included in the exception message and also available via explicit methods:

easygl::Shader vert(easygl::ShaderType::Vertex);
vert.create();
try {
    vert.compile_from_source(badSrc);
} catch (const easygl::Exception& e) {
    std::cerr << e.what() << '\n';
    // Or query directly:
    std::cerr << vert.info_log() << '\n';
}

Querying GL errors manually

You can poll for OpenGL errors using the Device:

easygl::ErrorCode err = device.get_error();
if (err != easygl::ErrorCode::NoError) {
    std::cerr << "GL error: " << static_cast<int>(err) << '\n';
}

OpenGL debug output

Enable the driver's debug callback (requires GL 4.3 or KHR_debug extension):

device.set_debug_output_enabled(true);
device.set_debug_output_synchronous_enabled(true);
// Then install a callback via raw GL (or meta-gl) if you need custom formatting

Context reset / robustness

On platforms that support it, you can check whether the GPU context was lost:

easygl::GraphicsResetStatus status = device.get_graphics_reset_status();
if (status != easygl::GraphicsResetStatus::NoError) {
    // context was lost — call reset_handle_no_gl() on all resources
    // then recreate them
}
Combine generation tracking (see Ownership) with get_graphics_reset_status() for robust context-loss recovery.

Feature checks before use

Check whether a feature is supported before using it to avoid driver-level errors:

if (device.supports(easygl::Feature::ComputeShader)) {
    device.dispatch_compute(64, 64, 1);
} else {
    // fallback path
}

// Or throw automatically if not supported:
device.require(easygl::Feature::FramebufferObject);