diff --git a/cgo.c b/cgo.c index 03d1175..aa1c712 100644 --- a/cgo.c +++ b/cgo.c @@ -15,3 +15,7 @@ int vm_set_output_callback(ph7_vm *pVm, void *pUserData) { int vm_create_function(ph7_vm *pVm, const char *zName, void *pUserData) { return ph7_create_function(pVm, zName, call_foreign_function, pUserData); } + +int vm_config_err_report(ph7_vm *pVm) { + return ph7_vm_config(pVm, PH7_VM_CONFIG_ERR_REPORT); +} \ No newline at end of file diff --git a/cgo.h b/cgo.h index c8576ab..cef8c52 100644 --- a/cgo.h +++ b/cgo.h @@ -3,6 +3,7 @@ int engine_config_err_log(ph7 *pEngine, const char **pzPtr, int *pLen); int vm_extract_output(ph7_vm *pVm, const char **pzPtr, int *pLen); +int vm_config_err_report(ph7_vm *pVm); extern int write_vm_output(void*, unsigned int, void*); int vm_set_output_callback(ph7_vm *pVm, void *pUserData); diff --git a/ph7.go b/ph7.go index e987e5e..312c461 100644 --- a/ph7.go +++ b/ph7.go @@ -210,7 +210,21 @@ func (c *Context) Write(p []byte) (int, error) { return len(p), nil } -// TODO (*Context).ThrowError +type Severity int + +const ( + SeverityErr Severity = C.PH7_CTX_ERR + SeverityWarning Severity = C.PH7_CTX_WARNING + SeverityNotice Severity = C.PH7_CTX_NOTICE +) + +func (c *Context) ThrowError(severity Severity, msg string) error { + cmsg := C.CString(msg) + defer C.free(unsafe.Pointer(cmsg)) + + return newError(C.ph7_context_throw_error((*C.ph7_context)(c), C.int(severity), cmsg)) +} + // TODO (*Context).ThrowErrorFormat maybe? func (c *Context) RandomNum() uint { @@ -382,13 +396,34 @@ func (vm *VM) SetOutputWriter(w io.Writer) error { return newError(C.vm_set_output_callback(vm.ptr, vm.pointerKey)) } -// TODO All the ph7_vm_config verbs +// TODO PH7_VM_CONFIG_IMPORT_PATH +// TODO PH7_VM_CONFIG_RECURSION_DEPTH +// TODO PH7_VM_CONFIG_CREATE_SUPER +// TODO PH7_VM_CONFIG_CREATE_VAR +// TODO PH7_VM_CONFIG_HTTP_REQUEST +// TODO PH7_VM_CONFIG_SERVER_ATTR +// TODO PH7_VM_CONFIG_ENV_ATTR +// TODO PH7_VM_CONFIG_SESSION_ATTR +// TODO PH7_VM_CONFIG_POST_ATTR +// TODO PH7_VM_CONFIG_GET_ATTR +// TODO PH7_VM_CONFIG_COOKIE_ATTR +// TODO PH7_VM_CONFIG_HEADER_ATTR +// TODO PH7_VM_CONFIG_EXEC_VALUE +// TODO PH7_VM_CONFIG_IO_STREAM +// TODO PH7_VM_CONFIG_ARGV_ENTRY +// TODO PH7_VM_CONFIG_EXTRACT_OUTPUT +// TODO PH7_VM_CONFIG_ERR_LOG_HANDLER + // TODO (*VM).Dump(w io.Writer) func (vm *VM) OutputWriteError() error { return vm.outputWriteErr } +func (vm *VM) ReportErrors() error { + return newError(C.vm_config_err_report((*C.ph7_vm)(vm.ptr))) +} + func (vm *VM) ExtractOutput() (string, error) { var s *C.char var n C.int diff --git a/ph7_test.go b/ph7_test.go index 028cc37..ba95f65 100644 --- a/ph7_test.go +++ b/ph7_test.go @@ -4,6 +4,7 @@ import ( "bytes" "errors" "fmt" + "strings" "testing" ph7 "git.codemonkeysoftware.net/b/go-ph7" @@ -178,6 +179,25 @@ func TestContextWrite(t *testing.T) { } } +func TestContextThrowError(t *testing.T) { + const script = "echo doStuff();" + vm, close := setupVM(t, script) + defer close() + + const msg = "aock.,rabkaeop" + f := func(ctx *ph7.Context) error { + return ctx.ThrowError(ph7.SeverityErr, msg) + } + mustSucceed(t, vm.CreateFunction("doStuff", CtxFunc(f))) + mustSucceed(t, vm.ReportErrors()) + mustSucceed(t, vm.Exec()) + output, err := vm.ExtractOutput() + mustSucceed(t, err) + if !strings.Contains(output, msg) { + t.Fatalf("expected output to contain %q, got %q", msg, output) + } +} + func TestContextResultInt(t *testing.T) { testResultCode(t, "42", func(ctx *ph7.Context) error { return ctx.ResultInt(42)