-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexception.v
More file actions
68 lines (58 loc) · 1.72 KB
/
exception.v
File metadata and controls
68 lines (58 loc) · 1.72 KB
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
module vphp
import vphp.zend
// 统一映射 zend 常量到 vphp 命名空间
pub const e_error = zend.e_error
pub const e_warning = zend.e_warning
pub enum InteropErrorClass {
worker_runtime_error
app_contract_error
invalid_argument
type_mismatch
conversion_error
include_error
symbol_not_found
}
pub fn (c InteropErrorClass) str() string {
return match c {
.worker_runtime_error { 'worker_runtime_error' }
.app_contract_error { 'app_contract_error' }
.invalid_argument { 'invalid_argument' }
.type_mismatch { 'type_mismatch' }
.conversion_error { 'conversion_error' }
.include_error { 'include_error' }
.symbol_not_found { 'symbol_not_found' }
}
}
// 抛出 PHP 异常
pub fn throw_exception(msg string, code int) {
unsafe { C.vphp_throw(&char(msg.str), code) }
}
pub fn throw_exception_class(class_name string, msg string, code int) {
unsafe { C.vphp_throw_class(&char(class_name.str), &char(msg.str), code) }
}
pub fn throw_exception_object(mut exception ZVal) {
if !exception.is_valid() || !exception.is_object() {
throw_exception('exception object must be a valid object', 0)
return
}
unsafe {
C.vphp_disown_zval(exception.raw)
C.vphp_throw_object(exception.raw)
}
exception.raw = unsafe { nil }
exception.owned = false
}
// 抛出带稳定错误分类前缀的 PHP 异常
pub fn throw_interop_error(class InteropErrorClass, msg string, code int) {
throw_exception('[${class.str()}] ${msg}', code)
}
// 将 V error 映射到 PHP exception,保留错误分类
pub fn throw_from_error(class InteropErrorClass, err IError, code int) {
throw_interop_error(class, err.msg(), code)
}
// 主动向 PHP 报告错误
pub fn report_error(level int, msg string) {
unsafe {
C.vphp_error(level, &char(msg.str))
}
}