Wrapped ph7_context_throw_error

This commit is contained in:
Brandon Dyck 2022-11-27 17:10:28 -07:00
parent b285c354d5
commit 57f15fdbd4
4 changed files with 62 additions and 2 deletions

4
cgo.c
View File

@ -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) { int vm_create_function(ph7_vm *pVm, const char *zName, void *pUserData) {
return ph7_create_function(pVm, zName, call_foreign_function, 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);
}

1
cgo.h
View File

@ -3,6 +3,7 @@
int engine_config_err_log(ph7 *pEngine, const char **pzPtr, int *pLen); 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_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*); extern int write_vm_output(void*, unsigned int, void*);
int vm_set_output_callback(ph7_vm *pVm, void *pUserData); int vm_set_output_callback(ph7_vm *pVm, void *pUserData);

39
ph7.go
View File

@ -210,7 +210,21 @@ func (c *Context) Write(p []byte) (int, error) {
return len(p), nil 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? // TODO (*Context).ThrowErrorFormat maybe?
func (c *Context) RandomNum() uint { 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)) 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) // TODO (*VM).Dump(w io.Writer)
func (vm *VM) OutputWriteError() error { func (vm *VM) OutputWriteError() error {
return vm.outputWriteErr 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) { func (vm *VM) ExtractOutput() (string, error) {
var s *C.char var s *C.char
var n C.int var n C.int

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"errors" "errors"
"fmt" "fmt"
"strings"
"testing" "testing"
ph7 "git.codemonkeysoftware.net/b/go-ph7" 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) { func TestContextResultInt(t *testing.T) {
testResultCode(t, "42", func(ctx *ph7.Context) error { testResultCode(t, "42", func(ctx *ph7.Context) error {
return ctx.ResultInt(42) return ctx.ResultInt(42)