diff --git a/ph7.go b/ph7.go index a347a58..d2a4bc7 100644 --- a/ph7.go +++ b/ph7.go @@ -56,22 +56,28 @@ type Error struct { // TODO create a CompileError that contains the error log text func newError(code C.int) error { - if code != C.PH7_OK { - return Error{ - Code: ResultCode(code), - } + if code == C.PH7_OK { + return nil + } + return Error{ + Code: ResultCode(code), } - return nil } func (e Error) Error() string { - return fmt.Sprintf("ph7: %s", e.Code) + return fmt.Sprintf("ph7: %s", e.Code.String()) +} + +type CompileError struct { + Msg string +} + +func (e CompileError) Error() string { + return fmt.Sprintf("ph7 compile error: %s", e.Msg) } type Engine C.ph7 -// TODO strip off the enclosing struct if possible after wrapping all engine methods. - func NewEngine() (*Engine, error) { var cengine *C.ph7 result := C.ph7_init(&cengine) @@ -96,6 +102,23 @@ func (e *Engine) ErrLog() (string, error) { // TODO All the ph7_config verbs +func (e *Engine) compileError(code C.int) error { + switch code { + case C.PH7_OK: + return nil + case C.PH7_COMPILE_ERR: + errLog, errLogErr := e.ErrLog() + if errLogErr != nil || errLog == "" { + return newError(code) + } + return CompileError{ + Msg: errLog, + } + default: + return newError(code) + } +} + func (e *Engine) Compile(source []byte, phpOnly bool) (*VM, error) { csource := C.CBytes(source) defer C.free(csource) @@ -104,7 +127,7 @@ func (e *Engine) Compile(source []byte, phpOnly bool) (*VM, error) { flags |= C.PH7_PHP_ONLY } var cvm *C.ph7_vm - err := newError(C.ph7_compile_v2((*C.ph7)(e), (*C.char)(csource), C.int(len(source)), &cvm, flags)) + err := e.compileError(C.ph7_compile_v2((*C.ph7)(e), (*C.char)(csource), C.int(len(source)), &cvm, flags)) if err != nil { return nil, err } diff --git a/ph7_test.go b/ph7_test.go index 079ba8f..3383dcf 100644 --- a/ph7_test.go +++ b/ph7_test.go @@ -2,6 +2,7 @@ package ph7_test import ( "bytes" + "errors" "fmt" "testing" @@ -63,10 +64,10 @@ func TestCompileError(t *testing.T) { _, err = engine.Compile([]byte(""), false) mustFail(t, err) - msg, err := engine.ErrLog() - mustSucceed(t, err) - if msg == "" { - t.Fatalf("expected error message, got nothing") + var target ph7.CompileError + isCompileError := errors.As(err, &target) + if !isCompileError || target.Msg == "" { + t.Fatalf("expected compile error, got %#v", err) } }