Skip to content

Commit 2417e6d

Browse files
committed
Improve shader compilation error handling
Previously we would ignore the error until we try to actually use the shader and get the very unhelpful: "t.uniformSetters is not defined"
1 parent ed45bcd commit 2417e6d

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/ShaderManager.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,22 @@ class ShaderManager {
6868
const fsFullText = definesText + require('raw-loader!./shaders/sprite.frag');
6969
/* eslint-enable global-require */
7070

71-
return twgl.createProgramInfo(this._gl, [vsFullText, fsFullText]);
71+
let errorMessage = null;
72+
const onError = (newError) => {
73+
// twgl won't log the error when we provide a custom error callback, so log it ourselves
74+
console.error(newError);
75+
76+
// For the error that we throw, just include the actual error from WebGL, not all the fancy
77+
// extras that twgl adds to the error messages.
78+
const match = newError.match(/\*\*\* Error compiling shader: ([\s\S]+)/);
79+
errorMessage = match ? match[1].trim() : newError;
80+
};
81+
82+
const program = twgl.createProgramInfo(this._gl, [vsFullText, fsFullText], null, null, onError);
83+
if (!program) {
84+
throw new Error(`Failed to compile shader (mode ${drawMode}, effects ${effectBits}): ${errorMessage}`);
85+
}
86+
return program;
7287
}
7388
}
7489

0 commit comments

Comments
 (0)