mirror of
https://github.com/yusing/godoxy.git
synced 2025-05-20 20:52:33 +02:00

* cleanup code for URL type * fix makefile for trace mode * refactor, merge Entry, RawEntry and Route into one. * Implement fileserver. * refactor: rename HTTPRoute to ReverseProxyRoute to avoid confusion * refactor: move metrics logger to middleware package - fix prometheus metrics for load balanced routes - route will now fail when health monitor fail to start * fix extra output of ls-* commands by defer initializaing stuff, speed up start time * add test for path traversal attack, small fix on FileServer.Start method * rename rule.on.bypass to pass * refactor and fixed map-to-map deserialization * updated route loading logic * schemas: add "add_prefix" option to modify_request middleware * updated route JSONMarshalling --------- Co-authored-by: yusing <yusing@6uo.me>
74 lines
1.3 KiB
Go
74 lines
1.3 KiB
Go
package err
|
|
|
|
import (
|
|
"fmt"
|
|
)
|
|
|
|
func newError(message string) error {
|
|
return errStr(message)
|
|
}
|
|
|
|
func New(message string) Error {
|
|
if message == "" {
|
|
return nil
|
|
}
|
|
return &baseError{newError(message)}
|
|
}
|
|
|
|
func Errorf(format string, args ...any) Error {
|
|
return &baseError{fmt.Errorf(format, args...)}
|
|
}
|
|
|
|
func Wrap(err error, message ...string) Error {
|
|
if len(message) == 0 || message[0] == "" {
|
|
return From(err)
|
|
}
|
|
return Errorf("%w: %s", err, message[0])
|
|
}
|
|
|
|
func From(err error) Error {
|
|
if err == nil {
|
|
return nil
|
|
}
|
|
//nolint:errorlint
|
|
switch err := err.(type) {
|
|
case *baseError:
|
|
return err
|
|
case *nestedError:
|
|
return err
|
|
}
|
|
return &baseError{err}
|
|
}
|
|
|
|
func Join(errors ...error) Error {
|
|
n := 0
|
|
for _, err := range errors {
|
|
if err != nil {
|
|
n++
|
|
}
|
|
}
|
|
if n == 0 {
|
|
return nil
|
|
}
|
|
errs := make([]error, n)
|
|
i := 0
|
|
for _, err := range errors {
|
|
if err != nil {
|
|
errs[i] = err
|
|
i++
|
|
}
|
|
}
|
|
return &nestedError{Extras: errs}
|
|
}
|
|
|
|
func Collect[T any, Err error, Arg any, Func func(Arg) (T, Err)](eb *Builder, fn Func, arg Arg) T {
|
|
result, err := fn(arg)
|
|
eb.Add(err)
|
|
return result
|
|
}
|
|
|
|
func Collect2[T any, Err error, Arg1 any, Arg2 any, Func func(Arg1, Arg2) (T, Err)](eb *Builder, fn Func, arg1 Arg1, arg2 Arg2) T {
|
|
result, err := fn(arg1, arg2)
|
|
eb.Add(err)
|
|
return result
|
|
}
|