-
-
Notifications
You must be signed in to change notification settings - Fork 219
/
error.go
89 lines (75 loc) · 2.37 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
package sdl
/*
#include "sdl_wrapper.h"
void GoSetError(const char *fmt) {
SDL_SetError("%s", fmt);
}
*/
// #include "sdl_wrapper.h"
import "C"
import "errors"
var emptyCString *C.char = C.CString("")
var ErrInvalidParameters = errors.New("Invalid Parameters")
// SDL error codes with their corresponding predefined strings.
const (
ENOMEM ErrorCode = C.SDL_ENOMEM // out of memory
EFREAD = C.SDL_EFREAD // error reading from datastream
EFWRITE = C.SDL_EFWRITE // error writing to datastream
EFSEEK = C.SDL_EFSEEK // error seeking in datastream
UNSUPPORTED = C.SDL_UNSUPPORTED // that operation is not supported
LASTERROR = C.SDL_LASTERROR // the highest numbered predefined error
)
// ErrorCode is an error code used in SDL error messages.
type ErrorCode uint32
type cErrorCode C.SDL_errorcode
func (ec ErrorCode) c() C.SDL_errorcode {
return C.SDL_errorcode(ec)
}
// GetError returns the last error that occurred, or an empty string if there hasn't been an error message set since the last call to ClearError().
// (https://wiki.libsdl.org/SDL_GetError)
func GetError() error {
if err := C.SDL_GetError(); err != nil {
gostr := C.GoString(err)
// SDL_GetError returns "an empty string if there hasn't been an error message"
if len(gostr) > 0 {
return errors.New(gostr)
}
}
return nil
}
// SetError set the SDL error message.
// (https://wiki.libsdl.org/SDL_SetError)
func SetError(err error) {
if err != nil {
C.GoSetError(C.CString(err.Error()))
return
}
C.GoSetError(emptyCString)
}
// ClearError clears any previous error message.
// (https://wiki.libsdl.org/SDL_ClearError)
func ClearError() {
C.SDL_ClearError()
}
// Error sets the SDL error message to the specified error code.
func Error(code ErrorCode) {
C.SDL_Error(code.c())
}
// OutOfMemory sets SDL error message to ENOMEM (out of memory).
func OutOfMemory() {
Error(ENOMEM)
}
// Unsupported sets SDL error message to UNSUPPORTED (that operation is not supported).
func Unsupported() {
Error(UNSUPPORTED)
}
// errorFromInt returns GetError() if passed negative value, otherwise it returns nil.
func errorFromInt(code int) (err error) {
if code < 0 {
err = GetError()
if err == nil {
err = errors.New("Unknown error (probably using old version of SDL2 and the function called is not supported?)")
}
}
return
}