1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

builtin: change IError msg and code to methods + fix vlib, add a deprecation notice for the old usages (#13041)

This commit is contained in:
Tim Basel
2022-02-11 14:52:33 +01:00
committed by GitHub
parent 61024d4b75
commit 9d0a5942ac
80 changed files with 493 additions and 324 deletions

View File

@@ -21,7 +21,7 @@ pub const (
)
pub fn socket_error_message(potential_code int, s string) ?int {
return socket_error(potential_code) or { return error('$err.msg; $s') }
return socket_error(potential_code) or { return error('$err.msg(); $s') }
}
pub fn socket_error(potential_code int) ?int {

View File

@@ -627,18 +627,25 @@ fn (mut h Header) add_key(key string) {
// Custom error struct for invalid header tokens
struct HeaderKeyError {
msg string
Error
code int
header string
invalid_char byte
}
pub fn (err HeaderKeyError) msg() string {
return "Invalid header key: '$err.header'"
}
pub fn (err HeaderKeyError) code() int {
return err.code
}
// is_valid checks if the header token contains all valid bytes
fn is_valid(header string) ? {
for _, c in header {
if int(c) >= 128 || !is_token(c) {
return IError(HeaderKeyError{
msg: "Invalid header key: '$header'"
code: 1
header: header
invalid_char: c
@@ -647,7 +654,6 @@ fn is_valid(header string) ? {
}
if header.len == 0 {
return IError(HeaderKeyError{
msg: "Invalid header key: '$header'"
code: 2
header: header
invalid_char: 0

View File

@@ -261,15 +261,20 @@ pub:
}
pub struct UnexpectedExtraAttributeError {
pub:
msg string
code int
Error
attributes []string
}
pub fn (err UnexpectedExtraAttributeError) msg() string {
return 'Encountered unexpected extra attributes: $err.attributes'
}
pub struct MultiplePathAttributesError {
pub:
msg string = 'Expected at most one path attribute'
code int
Error
}
pub fn (err MultiplePathAttributesError) msg() string {
return 'Expected at most one path attribute'
}
// multipart_form_body converts form and file data into a multipart/form

View File

@@ -48,7 +48,7 @@ pub fn (mut s Server) listen_and_serve() ? {
break
}
mut conn := s.listener.accept() or {
if err.msg != 'net: op timed out' {
if err.msg() != 'net: op timed out' {
eprintln('accept() failed: $err; skipping')
}
continue

View File

@@ -21,13 +21,13 @@ mut:
pub fn dial_tcp(address string) ?&TcpConn {
addrs := resolve_addrs_fuzzy(address, .tcp) or {
return error('$err.msg; could not resolve address $address in dial_tcp')
return error('$err.msg(); could not resolve address $address in dial_tcp')
}
// Very simple dialer
for addr in addrs {
mut s := new_tcp_socket(addr.family()) or {
return error('$err.msg; could not create new tcp socket in dial_tcp')
return error('$err.msg(); could not create new tcp socket in dial_tcp')
}
s.connect(addr) or {
// Connection failed
@@ -215,10 +215,10 @@ mut:
}
pub fn listen_tcp(family AddrFamily, saddr string) ?&TcpListener {
s := new_tcp_socket(family) or { return error('$err.msg; could not create new socket') }
s := new_tcp_socket(family) or { return error('$err.msg(); could not create new socket') }
addrs := resolve_addrs(saddr, family, .tcp) or {
return error('$err.msg; could not resolve address $saddr')
return error('$err.msg(); could not resolve address $saddr')
}
// TODO(logic to pick here)

View File

@@ -410,7 +410,7 @@ fn split_by_scheme(rawurl string) ?[]string {
}
fn get_scheme(rawurl string) ?string {
split := split_by_scheme(rawurl) or { return err.msg }
split := split_by_scheme(rawurl) or { return err.msg() }
return split[0]
}
@@ -593,9 +593,9 @@ fn parse_host(host string) ?string {
// We do impose some restrictions on the zone, to avoid stupidity
// like newlines.
if zone := host[..i].index('%25') {
host1 := unescape(host[..zone], .encode_host) or { return err.msg }
host2 := unescape(host[zone..i], .encode_zone) or { return err.msg }
host3 := unescape(host[i..], .encode_host) or { return err.msg }
host1 := unescape(host[..zone], .encode_host) or { return err.msg() }
host2 := unescape(host[zone..i], .encode_zone) or { return err.msg() }
host3 := unescape(host[i..], .encode_host) or { return err.msg() }
return host1 + host2 + host3
}
if idx := host.last_index(':') {
@@ -606,7 +606,7 @@ fn parse_host(host string) ?string {
}
}
}
h := unescape(host, .encode_host) or { return err.msg }
h := unescape(host, .encode_host) or { return err.msg() }
return h
// host = h
// return host

View File

@@ -122,7 +122,7 @@ fn (mut s Server) serve_client(mut c Client) ? {
}
s.setup_callbacks(mut server_client)
c.listen() or {
s.logger.error(err.msg)
s.logger.error(err.msg())
return err
}
}