Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: uniform error creation #668

Merged
merged 9 commits into from
May 19, 2023
Merged
4 changes: 2 additions & 2 deletions pkg/app/server/hertz.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@ package server

import (
"context"
"errors"
"os"
"os/signal"
"syscall"
"time"

"github.com/cloudwego/hertz/pkg/app/middlewares/server/recovery"
"github.com/cloudwego/hertz/pkg/common/config"
"github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/cloudwego/hertz/pkg/route"
)
Expand Down Expand Up @@ -108,7 +108,7 @@ func waitSignal(errCh chan error) error {
switch sig {
case syscall.SIGTERM:
// force exit
return errors.New(sig.String()) // nolint
return errors.NewPublic(sig.String()) // nolint
case syscall.SIGHUP, syscall.SIGINT:
hlog.SystemLogger().Infof("Received signal: %s\n", sig)
// graceful shutdown
Expand Down
4 changes: 2 additions & 2 deletions pkg/common/stackless/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
package stackless

import (
"errors"
"fmt"
"io"

"github.com/cloudwego/hertz/pkg/common/bytebufferpool"
"github.com/cloudwego/hertz/pkg/common/errors"
)

// Writer is an interface stackless writer must conform to.
Expand Down Expand Up @@ -137,7 +137,7 @@ func (w *writer) do(op op) error {
return err
}

var errHighLoad = errors.New("cannot compress data due to high load")
var errHighLoad = errors.NewPublic("cannot compress data due to high load")

var stacklessWriterFunc = NewFunc(writerFunc)

Expand Down
5 changes: 2 additions & 3 deletions pkg/common/test/mock/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package mock

import (
"bytes"
"errors"
"net"
"strings"
"time"
Expand Down Expand Up @@ -258,15 +257,15 @@ func (m *StreamConn) Peek(n int) ([]byte, error) {
m.Data = m.Data[:cap(m.Data)]
return m.Data[:1], nil
}
return nil, errors.New("not enough data")
return nil, errs.NewPublic("not enough data")
}

func (m *StreamConn) Skip(n int) error {
if len(m.Data) >= n {
m.Data = m.Data[n:]
return nil
}
return errors.New("not enough data")
return errs.NewPublic("not enough data")
}

func (m *StreamConn) Release() error {
Expand Down
7 changes: 4 additions & 3 deletions pkg/common/tracer/stats/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
package stats

import (
"errors"
"sync"
"sync/atomic"

"github.com/cloudwego/hertz/pkg/common/errors"
)

// EventIndex indicates a unique event.
Expand Down Expand Up @@ -88,8 +89,8 @@ var (

// errors
var (
ErrNotAllowed = errors.New("event definition is not allowed after initialization")
ErrDuplicated = errors.New("event name is already defined")
ErrNotAllowed = errors.NewPublic("event definition is not allowed after initialization")
ErrDuplicated = errors.NewPublic("event name is already defined")
)

var (
Expand Down
4 changes: 2 additions & 2 deletions pkg/protocol/http1/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ import (
"context"
"crypto/tls"
"encoding/base64"
"errors"
"time"

"github.com/cloudwego/hertz/internal/bytesconv"
"github.com/cloudwego/hertz/internal/bytestr"
"github.com/cloudwego/hertz/pkg/common/errors"
"github.com/cloudwego/hertz/pkg/network"
"github.com/cloudwego/hertz/pkg/protocol"
"github.com/cloudwego/hertz/pkg/protocol/consts"
Expand Down Expand Up @@ -111,7 +111,7 @@ func SetupProxy(conn network.Conn, addr string, proxyURI *protocol.URI, tlsConfi
if connectResp.StatusCode() != consts.StatusOK {
conn.Close()

return nil, errors.New(consts.StatusMessage(connectResp.StatusCode()))
return nil, errors.NewPublic(consts.StatusMessage(connectResp.StatusCode()))
}
}

Expand Down