Skip to content

Commit

Permalink
refactor error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Apr 8, 2017
1 parent 8175a75 commit 3524849
Show file tree
Hide file tree
Showing 141 changed files with 710 additions and 481 deletions.
3 changes: 3 additions & 0 deletions app/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package app

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg app -path App
13 changes: 7 additions & 6 deletions app/dispatcher/impl/default.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package impl

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg impl -path App,Dispatcher,Default

import (
"context"

Expand All @@ -9,7 +11,6 @@ import (
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/router"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/ray"
Expand All @@ -23,13 +24,13 @@ type DefaultDispatcher struct {
func NewDefaultDispatcher(ctx context.Context, config *dispatcher.Config) (*DefaultDispatcher, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("no space in context").Path("App", "Dispatcher", "Default")
return nil, newError("no space in context")
}
d := &DefaultDispatcher{}
space.OnInitialize(func() error {
d.ohm = proxyman.OutboundHandlerManagerFromSpace(space)
if d.ohm == nil {
return errors.New("OutboundHandlerManager is not found in the space").Path("App", "Dispatcher", "Default")
return newError("OutboundHandlerManager is not found in the space")
}
d.router = router.FromSpace(space)
return nil
Expand Down Expand Up @@ -58,13 +59,13 @@ func (v *DefaultDispatcher) Dispatch(ctx context.Context, destination net.Destin
if v.router != nil {
if tag, err := v.router.TakeDetour(ctx); err == nil {
if handler := v.ohm.GetHandler(tag); handler != nil {
log.Trace(errors.New("taking detour [", tag, "] for [", destination, "]").Path("App", "Dispatcher", "Default"))
log.Trace(newError("taking detour [", tag, "] for [", destination, "]"))
dispatcher = handler
} else {
log.Trace(errors.New("nonexisting tag: ", tag).AtWarning().Path("App", "Dispatcher", "Default"))
log.Trace(newError("nonexisting tag: ", tag).AtWarning())
}
} else {
log.Trace(errors.New("default route for ", destination).Path("App", "Dispatcher", "Default"))
log.Trace(newError("default route for ", destination))
}
}

Expand Down
7 changes: 7 additions & 0 deletions app/dispatcher/impl/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package impl

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Dispatcher", "Default")
}
3 changes: 1 addition & 2 deletions app/dns/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import (
"net"

"v2ray.com/core/app/log"
"v2ray.com/core/common/errors"
)

func (v *Config) GetInternalHosts() map[string]net.IP {
hosts := make(map[string]net.IP)
for domain, ipOrDomain := range v.GetHosts() {
address := ipOrDomain.AsAddress()
if address.Family().IsDomain() {
log.Trace(errors.New("ignoring domain address in static hosts: ", address.Domain()).AtWarning().Path("App", "DNS", "Config"))
log.Trace(newError("ignoring domain address in static hosts: ", address.Domain()).AtWarning())
continue
}
hosts[domain] = address.IP()
Expand Down
2 changes: 2 additions & 0 deletions app/dns/dns.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package dns

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg dns -path App,DNS

import (
"net"

Expand Down
5 changes: 5 additions & 0 deletions app/dns/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dns

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "DNS") }
7 changes: 7 additions & 0 deletions app/dns/server/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package server

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "DNS", "Server")
}
9 changes: 4 additions & 5 deletions app/dns/server/nameserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/transport/internet/udp"
)
Expand Down Expand Up @@ -89,7 +88,7 @@ func (v *UDPNameServer) AssignUnusedID(response chan<- *ARecord) uint16 {
if _, found := v.requests[id]; found {
continue
}
log.Trace(errors.New("add pending request id ", id).AtDebug().Path("App", "DNS", "UDPNameServer"))
log.Trace(newError("add pending request id ", id).AtDebug())
v.requests[id] = &PendingRequest{
expire: time.Now().Add(time.Second * 8),
response: response,
Expand All @@ -105,15 +104,15 @@ func (v *UDPNameServer) HandleResponse(payload *buf.Buffer) {
msg := new(dns.Msg)
err := msg.Unpack(payload.Bytes())
if err != nil {
log.Trace(errors.New("failed to parse DNS response").Base(err).AtWarning().Path("App", "DNS", "UDPNameServer"))
log.Trace(newError("failed to parse DNS response").Base(err).AtWarning())
return
}
record := &ARecord{
IPs: make([]net.IP, 0, 16),
}
id := msg.Id
ttl := DefaultTTL
log.Trace(errors.New("handling response for id ", id, " content: ", msg.String()).AtDebug().Path("App", "DNS", "UDPNameServer"))
log.Trace(newError("handling response for id ", id, " content: ", msg.String()).AtDebug())

v.Lock()
request, found := v.requests[id]
Expand Down Expand Up @@ -201,7 +200,7 @@ func (v *LocalNameServer) QueryA(domain string) <-chan *ARecord {

ips, err := net.LookupIP(domain)
if err != nil {
log.Trace(errors.New("failed to lookup IPs for domain ", domain).Path("App", "DNS", "LocalNameServer").Base(err))
log.Trace(newError("failed to lookup IPs for domain ", domain).Base(err))
return
}

Expand Down
11 changes: 6 additions & 5 deletions app/dns/server/server.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package server

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg server -path App,DNS,Server

import (
"context"
"net"
Expand All @@ -12,7 +14,6 @@ import (
"v2ray.com/core/app/dns"
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
)

Expand All @@ -35,7 +36,7 @@ type CacheServer struct {
func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, error) {
space := app.SpaceFromContext(ctx)
if space == nil {
return nil, errors.New("no space in context").Path("App", "DNS", "CacheServer")
return nil, newError("no space in context")
}
server := &CacheServer{
records: make(map[string]*DomainRecord),
Expand All @@ -45,7 +46,7 @@ func NewCacheServer(ctx context.Context, config *dns.Config) (*CacheServer, erro
space.OnInitialize(func() error {
disp := dispatcher.FromSpace(space)
if disp == nil {
return errors.New("dispatcher is not found in the space").Path("App", "DNS", "CacheServer")
return newError("dispatcher is not found in the space")
}
for idx, destPB := range config.NameServers {
address := destPB.Address.AsAddress()
Expand Down Expand Up @@ -113,13 +114,13 @@ func (v *CacheServer) Get(domain string) []net.IP {
A: a,
}
v.Unlock()
log.Trace(errors.New("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug().Path("App", "DNS", "CacheServer"))
log.Trace(newError("returning ", len(a.IPs), " IPs for domain ", domain).AtDebug())
return a.IPs
case <-time.After(QueryTimeout):
}
}

log.Trace(errors.New("returning nil for domain ", domain).AtDebug().Path("App", "DNS", "CacheServer"))
log.Trace(newError("returning nil for domain ", domain).AtDebug())
return nil
}

Expand Down
5 changes: 5 additions & 0 deletions app/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package app

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App") }
7 changes: 2 additions & 5 deletions app/log/access.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package log

import (
"v2ray.com/core/app/log/internal"
"v2ray.com/core/common/errors"
)
import "v2ray.com/core/app/log/internal"

// AccessStatus is the status of an access request from clients.
type AccessStatus string
Expand All @@ -21,7 +18,7 @@ var (
func InitAccessLogger(file string) error {
logger, err := internal.NewFileLogWriter(file)
if err != nil {
return errors.New("failed to create access logger on file: ", file).Base(err).Path("App", "Log")
return newError("failed to create access logger on file: ", file).Base(err)
}
accessLoggerInstance = logger
return nil
Expand Down
5 changes: 5 additions & 0 deletions app/log/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package log

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("App", "Log") }
4 changes: 3 additions & 1 deletion app/log/log.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package log

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg log -path App,Log

import (
"context"

Expand Down Expand Up @@ -43,7 +45,7 @@ func SetLogLevel(level LogLevel) {
func InitErrorLogger(file string) error {
logger, err := internal.NewFileLogWriter(file)
if err != nil {
return errors.New("failed to create error logger on file (", file, ")").Base(err).Path("App", "Log")
return newError("failed to create error logger on file (", file, ")").Base(err)
}
streamLoggerInstance = logger
return nil
Expand Down
3 changes: 1 addition & 2 deletions app/proxyman/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package proxyman
import (
"context"

"v2ray.com/core/common/errors"
"v2ray.com/core/proxy"
)

Expand All @@ -23,7 +22,7 @@ func (s *AllocationStrategy) GetRefreshValue() uint32 {

func (c *OutboundHandlerConfig) GetProxyHandler(ctx context.Context) (proxy.Outbound, error) {
if c == nil {
return nil, errors.New("OutboundHandlerConfig is nil").Path("App", "Proxyman", "Outbound", "OutboundHandlerConfig")
return nil, newError("OutboundHandlerConfig is nil")
}
config, err := c.ProxySettings.GetInstance()
if err != nil {
Expand Down
7 changes: 7 additions & 0 deletions app/proxyman/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package proxyman

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman")
}
3 changes: 1 addition & 2 deletions app/proxyman/inbound/always.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/mux"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/proxy"
)
Expand Down Expand Up @@ -37,7 +36,7 @@ func NewAlwaysOnInboundHandler(ctx context.Context, tag string, receiverConfig *
}
for port := pr.From; port <= pr.To; port++ {
if nl.HasNetwork(net.Network_TCP) {
log.Trace(errors.New("creating tcp worker on ", address, ":", port).AtDebug().Path("App", "Proxyman", "Inbound", "AlwaysOnInboundHandler"))
log.Trace(newError("creating tcp worker on ", address, ":", port).AtDebug())
worker := &tcpWorker{
address: address,
port: net.Port(port),
Expand Down
7 changes: 3 additions & 4 deletions app/proxyman/inbound/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"v2ray.com/core/app/proxyman"
"v2ray.com/core/app/proxyman/mux"
"v2ray.com/core/common/dice"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
)
Expand Down Expand Up @@ -93,7 +92,7 @@ func (h *DynamicInboundHandler) refresh() error {
port := h.allocatePort()
p, err := proxy.CreateInboundHandler(ctx, h.proxyConfig)
if err != nil {
log.Trace(errors.New("failed to create proxy instance").Base(err).Path("App", "Proxyman", "Inbound", "DynamicInboundHandler").AtWarning())
log.Trace(newError("failed to create proxy instance").Base(err).AtWarning())
continue
}
nl := p.Network()
Expand All @@ -108,7 +107,7 @@ func (h *DynamicInboundHandler) refresh() error {
dispatcher: h.mux,
}
if err := worker.Start(); err != nil {
log.Trace(errors.New("failed to create TCP worker").Base(err).AtWarning().Path("App", "Proxyman", "Inbound", "DynamicInboundHandler"))
log.Trace(newError("failed to create TCP worker").Base(err).AtWarning())
continue
}
workers = append(workers, worker)
Expand All @@ -124,7 +123,7 @@ func (h *DynamicInboundHandler) refresh() error {
dispatcher: h.mux,
}
if err := worker.Start(); err != nil {
log.Trace(errors.New("failed to create UDP worker").Base(err).AtWarning().Path("App", "Proxyman", "Inbound", "DynamicInboundHandler"))
log.Trace(newError("failed to create UDP worker").Base(err).AtWarning())
continue
}
workers = append(workers, worker)
Expand Down
7 changes: 7 additions & 0 deletions app/proxyman/inbound/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package inbound

import "v2ray.com/core/common/errors"

func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("App", "Proxyman", "Inbound")
}
9 changes: 5 additions & 4 deletions app/proxyman/inbound/inbound.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package inbound

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg inbound -path App,Proxyman,Inbound

import (
"context"

"v2ray.com/core/app/proxyman"
"v2ray.com/core/common"
"v2ray.com/core/common/errors"
)

type DefaultInboundHandlerManager struct {
Expand All @@ -26,7 +27,7 @@ func (m *DefaultInboundHandlerManager) AddHandler(ctx context.Context, config *p
}
receiverSettings, ok := rawReceiverSettings.(*proxyman.ReceiverConfig)
if !ok {
return errors.New("not a ReceiverConfig").Path("App", "Proxyman", "Inbound", "DefaultInboundHandlerManager")
return newError("not a ReceiverConfig")
}
proxySettings, err := config.ProxySettings.GetInstance()
if err != nil {
Expand All @@ -50,7 +51,7 @@ func (m *DefaultInboundHandlerManager) AddHandler(ctx context.Context, config *p
}

if handler == nil {
return errors.New("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type).Path("App", "Proxyman", "Inbound", "DefaultInboundHandlerManager")
return newError("unknown allocation strategy: ", receiverSettings.AllocationStrategy.Type)
}

m.handlers = append(m.handlers, handler)
Expand All @@ -63,7 +64,7 @@ func (m *DefaultInboundHandlerManager) AddHandler(ctx context.Context, config *p
func (m *DefaultInboundHandlerManager) GetHandler(ctx context.Context, tag string) (proxyman.InboundHandler, error) {
handler, found := m.taggedHandlers[tag]
if !found {
return nil, errors.New("handler not found: ", tag).Path("App", "Proxyman", "Inbound", "DefaultInboundHandlerManager")
return nil, newError("handler not found: ", tag)
}
return handler, nil
}
Expand Down
5 changes: 2 additions & 3 deletions app/proxyman/inbound/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"v2ray.com/core/app/dispatcher"
"v2ray.com/core/app/log"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
v2net "v2ray.com/core/common/net"
"v2ray.com/core/proxy"
"v2ray.com/core/transport/internet"
Expand Down Expand Up @@ -54,7 +53,7 @@ func (w *tcpWorker) callback(conn internet.Connection) {
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.TCPDestination(w.address, w.port))
ctx = proxy.ContextWithSource(ctx, v2net.DestinationFromAddr(conn.RemoteAddr()))
if err := w.proxy.Process(ctx, v2net.Network_TCP, conn, w.dispatcher); err != nil {
log.Trace(errors.New("connection ends").Base(err).Path("App", "Proxyman", "Inbound", "TCPWorker"))
log.Trace(newError("connection ends").Base(err))
}
cancel()
conn.Close()
Expand Down Expand Up @@ -231,7 +230,7 @@ func (w *udpWorker) callback(b *buf.Buffer, source v2net.Destination, originalDe
ctx = proxy.ContextWithSource(ctx, source)
ctx = proxy.ContextWithInboundEntryPoint(ctx, v2net.UDPDestination(w.address, w.port))
if err := w.proxy.Process(ctx, v2net.Network_UDP, conn, w.dispatcher); err != nil {
log.Trace(errors.New("connection ends").Base(err).Path("App", "Proxymann", "Inbound", "UDPWorker"))
log.Trace(newError("connection ends").Base(err))
}
w.removeConn(source)
cancel()
Expand Down
Loading

0 comments on commit 3524849

Please sign in to comment.