forked from go-gl/glfw
/
error.go
200 lines (180 loc) · 6.63 KB
/
error.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
package glfw
//#define GLFW_INCLUDE_NONE
//#include "glfw/include/GLFW/glfw3.h"
//void glfwSetErrorCallbackCB();
import "C"
import (
"fmt"
"log"
)
// ErrorCode corresponds to an error code.
type ErrorCode int
// Error codes that are translated to panics and the programmer should not
// expect to handle.
const (
notInitialized ErrorCode = C.GLFW_NOT_INITIALIZED // GLFW has not been initialized.
noCurrentContext ErrorCode = C.GLFW_NO_CURRENT_CONTEXT // No context is current.
invalidEnum ErrorCode = C.GLFW_INVALID_ENUM // One of the enum parameters for the function was given an invalid enum.
invalidValue ErrorCode = C.GLFW_INVALID_VALUE // One of the parameters for the function was given an invalid value.
outOfMemory ErrorCode = C.GLFW_OUT_OF_MEMORY // A memory allocation failed.
platformError ErrorCode = C.GLFW_PLATFORM_ERROR // A platform-specific error occurred that does not match any of the more specific categories.
)
const (
// APIUnavailable is the error code used when GLFW could not find support
// for the requested client API on the system.
//
// The installed graphics driver does not support the requested client API,
// or does not support it via the chosen context creation backend. Below
// are a few examples.
//
// Some pre-installed Windows graphics drivers do not support OpenGL. AMD
// only supports OpenGL ES via EGL, while Nvidia and Intel only supports it
// via a WGL or GLX extension. OS X does not provide OpenGL ES at all. The
// Mesa EGL, OpenGL and OpenGL ES libraries do not interface with the
// Nvidia binary driver.
APIUnavailable ErrorCode = C.GLFW_API_UNAVAILABLE
// VersionUnavailable is the error code used when the requested OpenGL or
// OpenGL ES (including any requested profile or context option) is not
// available on this machine.
//
// The machine does not support your requirements. If your application is
// sufficiently flexible, downgrade your requirements and try again.
// Otherwise, inform the user that their machine does not match your
// requirements.
//
// Future invalid OpenGL and OpenGL ES versions, for example OpenGL 4.8 if
// 5.0 comes out before the 4.x series gets that far, also fail with this
// error and not GLFW_INVALID_VALUE, because GLFW cannot know what future
// versions will exist.
VersionUnavailable ErrorCode = C.GLFW_VERSION_UNAVAILABLE
// FormatUnavailable is the error code used for both window creation and
// clipboard querying format errors.
//
// If emitted during window creation, the requested pixel format is not
// supported. This means one or more hard constraints did not match any of
// the available pixel formats. If your application is sufficiently
// flexible, downgrade your requirements and try again. Otherwise, inform
// the user that their machine does not match your requirements.
//
// If emitted when querying the clipboard, the contents of the clipboard
// could not be converted to the requested format. You should ignore the
// error or report it to the user, as appropriate.
FormatUnavailable ErrorCode = C.GLFW_FORMAT_UNAVAILABLE
)
func (e ErrorCode) String() string {
switch e {
case notInitialized:
return "NotInitialized"
case noCurrentContext:
return "NoCurrentContext"
case invalidEnum:
return "InvalidEnum"
case invalidValue:
return "InvalidValue"
case outOfMemory:
return "OutOfMemory"
case platformError:
return "PlatformError"
case APIUnavailable:
return "APIUnavailable"
case VersionUnavailable:
return "VersionUnavailable"
case FormatUnavailable:
return "FormatUnavailable"
default:
return fmt.Sprintf("ErrorCode(%d)", e)
}
}
// Error holds error code and description.
type Error struct {
Code ErrorCode
Desc string
}
// Error prints the error code and description in a readable format.
func (e *Error) Error() string {
return fmt.Sprintf("%s: %s", e.Code.String(), e.Desc)
}
// Note: There are many cryptic caveats to proper error handling here.
// See: https://github.com/HACKERALERT/glfw3/pull/86
// Holds the value of the last error.
var lastError = make(chan *Error, 1)
//export goErrorCB
func goErrorCB(code C.int, desc *C.char) {
flushErrors()
err := &Error{ErrorCode(code), C.GoString(desc)}
select {
case lastError <- err:
default:
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// Set the glfw callback internally
func init() {
C.glfwSetErrorCallbackCB()
}
// flushErrors is called by Terminate before it actually calls C.glfwTerminate,
// this ensures that any uncaught errors buffered in lastError are printed
// before the program exits.
func flushErrors() {
err := fetchError()
if err != nil {
fmt.Println("GLFW: An uncaught error has occurred:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
}
}
// acceptError fetches the next error from the error channel, it accepts only
// errors with one of the given error codes. If any other error is encountered,
// a panic will occur.
//
// Platform errors are always printed, for information why please see:
//
// https://github.com/HACKERALERT/glfw/issues/127
//
func acceptError(codes ...ErrorCode) error {
// Grab the next error, if there is one.
err := fetchError()
if err == nil {
return nil
}
// Only if the error has the specific error code accepted by the caller, do
// we return the error.
for _, code := range codes {
if err.Code == code {
return err
}
}
// The error isn't accepted by the caller. If the error code is not a code
// defined in the GLFW C documentation as a programmer error, then the
// caller should have accepted it. This is effectively a bug in this
// package.
switch err.Code {
case platformError:
log.Println(err)
return nil
case notInitialized, noCurrentContext, invalidEnum, invalidValue, outOfMemory:
panic(err)
default:
fmt.Println("GLFW: An invalid error was not accepted by the caller:", err)
fmt.Println("GLFW: Please report this bug in the Go package immediately.")
panic(err)
}
}
// panicError is a helper used by functions which expect no errors (except
// programmer errors) to occur. It will panic if it finds any such error.
func panicError() {
err := acceptError()
if err != nil {
panic(err)
}
}
// fetchError fetches the next error from the error channel, it does not block
// and returns nil if there is no error present.
func fetchError() *Error {
select {
case err := <-lastError:
return err
default:
return nil
}
}