gl-format-compiler-error

raw JSON →
1.0.3 verified Fri May 01 auth: no javascript

Formats WebGL GLSL compiler errors with code snippets and shader type context. Current stable version 1.0.3, no active release cadence (last updated 2015). Key differentiator: produces both a long human-readable error with source lines and a short error string. Works with glsl-shader-name for shader naming. Lightweight and focused on WebGL 1.0 error formatting.

error TypeError: formatCompilerError is not a function
cause Using ES import syntax with a CJS-only package.
fix
Use require('gl-format-compiler-error') or configure bundler to handle CJS.
gotcha The 'source' argument must be the exact shader source string passed to gl.shaderSource. Using a different string may produce incorrect line numbers.
fix Pass the exact source string variable used in gl.shaderSource.
gotcha The 'type' argument expects gl.VERTEX_SHADER or gl.FRAGMENT_SHADER (numeric constants). Passing a different value may produce mislabeled error messages.
fix Use gl.VERTEX_SHADER or gl.FRAGMENT_SHADER.
npm install gl-format-compiler-error
yarn add gl-format-compiler-error
pnpm add gl-format-compiler-error

Shows basic usage: compile a shader, capture error, format using the library.

var formatCompilerError = require('gl-format-compiler-error');
var gl = document.createElement('canvas').getContext('webgl');
var shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(shader, 'void main() { gl_Position = vec4(1.0); }');
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
  var errLog = gl.getShaderInfoLog(shader);
  var fmt = formatCompilerError(errLog, shaderSource, gl.VERTEX_SHADER);
  console.warn(fmt.long);
  throw new Error(fmt.short);
}