mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: panic with error message when go command fails (#10943)
This commit is contained in:
@@ -3,6 +3,7 @@ module builtin
|
||||
type FnExitCb = fn ()
|
||||
|
||||
fn C.atexit(f FnExitCb) int
|
||||
fn C.strerror(int) &char
|
||||
|
||||
[noreturn]
|
||||
fn vhalt() {
|
||||
@@ -100,6 +101,28 @@ pub fn panic(s string) {
|
||||
vhalt()
|
||||
}
|
||||
|
||||
// return a C-API error message matching to `errnum`
|
||||
pub fn c_error_number_str(errnum int) string {
|
||||
mut err_msg := ''
|
||||
$if freestanding {
|
||||
err_msg = 'error $errnum'
|
||||
} $else {
|
||||
c_msg := C.strerror(errnum)
|
||||
err_msg = string{
|
||||
str: &byte(c_msg)
|
||||
len: unsafe { C.strlen(c_msg) }
|
||||
is_lit: 1
|
||||
}
|
||||
}
|
||||
return err_msg
|
||||
}
|
||||
|
||||
// panic with a C-API error message matching `errnum`
|
||||
[noreturn]
|
||||
pub fn panic_error_number(basestr string, errnum int) {
|
||||
panic(basestr + c_error_number_str(errnum))
|
||||
}
|
||||
|
||||
// eprintln prints a message with a line end, to stderr. Both stderr and stdout are flushed.
|
||||
pub fn eprintln(s string) {
|
||||
if s.str == 0 {
|
||||
|
||||
@@ -134,3 +134,11 @@ fn break_if_debugger_attached() {
|
||||
_ = ptr
|
||||
}
|
||||
}
|
||||
|
||||
// These functions are Windows specific - provide dummys for *nix
|
||||
pub fn winapi_lasterr_str() string {
|
||||
return ''
|
||||
}
|
||||
|
||||
[noreturn]
|
||||
pub fn panic_lasterr() {}
|
||||
|
||||
@@ -54,6 +54,8 @@ fn C.SymFromAddr(h_process voidptr, address u64, p_displacement voidptr, p_symbo
|
||||
|
||||
fn C.SymGetLineFromAddr64(h_process voidptr, address u64, p_displacement voidptr, p_line &Line64) int
|
||||
|
||||
fn C.FormatMessage(dwFlags u32, lpSource voidptr, dwMessageId u32, dwLanguageId u32, lpBuffer &voidptr, nSize u32, Arguments voidptr) u32
|
||||
|
||||
// Ref - https://docs.microsoft.com/en-us/windows/win32/api/dbghelp/nf-dbghelp-symsetoptions
|
||||
const (
|
||||
symopt_undname = 0x00000002
|
||||
@@ -277,3 +279,26 @@ fn break_if_debugger_attached() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// return an error message generated from WinAPI's `LastError`
|
||||
pub fn winapi_lasterr_str() string {
|
||||
mut msgbuf := &byte(0)
|
||||
err_msg_id := C.GetLastError()
|
||||
res := C.FormatMessage(C.FORMAT_MESSAGE_ALLOCATE_BUFFER | C.FORMAT_MESSAGE_FROM_SYSTEM,
|
||||
C.NULL, err_msg_id, 0, &msgbuf, 0, C.NULL)
|
||||
err_msg := if res == 0 {
|
||||
'unknown error $err_msg_id'
|
||||
} else {
|
||||
string{
|
||||
str: msgbuf
|
||||
len: int(res)
|
||||
}
|
||||
}
|
||||
return err_msg
|
||||
}
|
||||
|
||||
// panic with an error message generated from WinAPI's `LastError`
|
||||
[noreturn]
|
||||
pub fn panic_lasterr(base string) {
|
||||
panic(base + winapi_lasterr_str())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user