improve error reporting; add GLError type

This commit is contained in:
faiface 2016-11-25 17:22:25 +01:00
parent 5043d06295
commit 0986e0c35e
1 changed files with 24 additions and 21 deletions

View File

@ -1,7 +1,7 @@
package pixelgl
import (
"errors"
"fmt"
"runtime"
"github.com/go-gl/gl/v3.3-core/gl"
@ -131,6 +131,28 @@ func DoValGLErr(f func() interface{}) (_ interface{}, gl error) {
return <-val, <-glerr
}
// GLError represents an error code generated by OpenGL.
type GLError uint32
// Error returns a human-readable textual representation of an OpenGL error.
func (err GLError) Error() string {
if desc, ok := glErrors[uint32(err)]; ok {
return fmt.Sprintf("OpenGL error: %s", desc)
}
return fmt.Sprintf("OpenGL error: unknown error")
}
var glErrors = map[uint32]string{
gl.INVALID_ENUM: "invalid enum",
gl.INVALID_VALUE: "invalid value",
gl.INVALID_OPERATION: "invalid operation",
gl.STACK_OVERFLOW: "stack overflow",
gl.STACK_UNDERFLOW: "stack underflow",
gl.OUT_OF_MEMORY: "out of memory",
gl.INVALID_FRAMEBUFFER_OPERATION: "invalid framebuffer operation",
gl.CONTEXT_LOST: "context lost",
}
// getLastGLErr returns (and consumes) the last error generated by OpenGL.
// Don't use outside DoGLErr, DoErrGLErr and DoValGLErr.
func getLastGLErr() error {
@ -141,24 +163,5 @@ func getLastGLErr() error {
if err == gl.NO_ERROR {
return nil
}
switch err {
case gl.INVALID_ENUM:
return errors.New("invalid enum")
case gl.INVALID_VALUE:
return errors.New("invalid value")
case gl.INVALID_OPERATION:
return errors.New("invalid operation")
case gl.STACK_OVERFLOW:
return errors.New("stack overflow")
case gl.STACK_UNDERFLOW:
return errors.New("stack underflow")
case gl.OUT_OF_MEMORY:
return errors.New("out of memory")
case gl.INVALID_FRAMEBUFFER_OPERATION:
return errors.New("invalid framebuffer operation")
case gl.CONTEXT_LOST:
return errors.New("context lost")
default:
return errors.New("unknown error")
}
return GLError(err)
}