[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Public WebGL] getContext error handling

The error handling for canvas.getContext("webgl") is inconsistent with other web APIs.  Synchronous APIs, like FileReaderSync and IDBDatabaseSync, report errors by throwing exceptions with error information.  Asynchronous APIs, like FileReader and IDBDatabase, report errors by firing an event.  getContext is a synchronous API, but WebGL is reporting errors using the event mechanism used by asynchronous APIs.  It should be throwing an exception instead.

A related issue: the description of webglcontextcreationerror is vague.  It says "Later, at the normal event delivery time".  Events can be dispatched either in the event loop (async exceptions) or immediately (sync exceptions); there's no such thing as the "normal event delivery time".  It sounds like this was written under the assumption that all events are asynchronous.  HTML's terminology for this is concise and precise: "fire a simple event" to fire an event immediately, and "queue a task to fire a simple event" to fire an event later from the event loop.

If it's too late to replace webglcontextcreationerror with an exception, note that this event should be fired synchronously, not asynchronously, so it's possible to retrieve error information as a result of a getContext call immediately.  For example:

    var error = "";
    var retrieveError = function(e) {  error = e.statusMessage || "unknown error"; };
    canvas.addEventListener("webglcontextcreationerror", retrieveError, false);
    var ctx = canvas.getContext("experimental-webgl");
    canvas.removeEventListener("webglcontextcreationerror", retrieveError, false);
        alert("WebGL error: " + error);

Note that this event is already synchronous in Chrome, so the above code already works there.

Glenn Maynard