Is there a problem with passing a single error to errors.Join?

WBOY
Release: 2024-02-09 11:03:07
forward
1090 people have browsed it

将单个错误传递给errors.Join 有问题吗?

When php editor Xinyi solves error handling, he often encounters the problem of passing a single error to errors.Join. This method is used to splice multiple error messages into a string to facilitate recording and viewing of error logs. However, we need to note that if there are too many error messages or are too long, the spliced ​​string may exceed the system limit, causing part of the error message to be lost. Therefore, when using errors.Join, we need to carefully consider the number and length of error messages to ensure complete recording and troubleshooting of errors.

Question content

go 1.20 introduces the errors.join function, which can wrap multiple errors. Are there any issues with calling this function and only passing an error?

For example, this article recommends against using the defer f.close() idiom for writable files, as this will silently ignore any errors returned by close. Instead, it is recommended to use the named return value err to allow the return value of close to be propagated - unless doing so would overwrite the previous error:

defer func() {
    cerr := f.close()
    if err == nil {
        err = cerr
    }
}()
Copy after login

It seems more correct to use errors.join in this case:

defer func() {
    cerr := f.Close()
    err = errors.Join(err, cerr)
}()
Copy after login

If both err and cerr are non-zero, two errors will now be returned. If they are all nil, return nil.

However, if one is nil and the other is non-nil, then errors.join will not only return the non-nil error, Also returns an errors.joinerror wrapper around it. Will packaging errors like this cause any problems? Especially if multiple functions in the call stack use this approach, a single error might end up in multiple layers of wrappers?

Workaround

If errors.joinerror has only one non-zero error, that is still a join error, and errors.as and errors.is function works as expected. This is true regardless of the nesting level of the connection error.

The only potential problem is if you have code like this:

err:=someFunc()
if err==io.EOF {
  ...
}
Copy after login

Then this will fail. This code must be rewritten to use errors.is.

The above is the detailed content of Is there a problem with passing a single error to errors.Join?. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!