Skip to content

Commit

Permalink
error generator
Browse files Browse the repository at this point in the history
  • Loading branch information
DarienRaymond committed Apr 8, 2017
1 parent b943581 commit 503f767
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 10 deletions.
19 changes: 9 additions & 10 deletions proxy/shadowsocks/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"v2ray.com/core/app/log"
"v2ray.com/core/common"
"v2ray.com/core/common/buf"
"v2ray.com/core/common/errors"
"v2ray.com/core/common/net"
"v2ray.com/core/common/protocol"
"v2ray.com/core/common/retry"
Expand Down Expand Up @@ -40,7 +39,7 @@ func NewClient(ctx context.Context, config *ClientConfig) (*Client, error) {
func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, dialer proxy.Dialer) error {
destination, ok := proxy.TargetFromContext(ctx)
if !ok {
return errors.New("target not specified").Path("Proxy", "Shadowsocks", "Client")
return newError("target not specified")
}
network := destination.Network

Expand All @@ -60,9 +59,9 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
return nil
})
if err != nil {
return errors.New("failed to find an available destination").AtWarning().Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("failed to find an available destination").AtWarning().Base(err)
}
log.Trace(errors.New("tunneling request to ", destination, " via ", server.Destination()).Path("Proxy", "Shadowsocks", "Client"))
log.Trace(newError("tunneling request to ", destination, " via ", server.Destination()))

defer conn.Close()

Expand All @@ -80,7 +79,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
user := server.PickUser()
rawAccount, err := user.GetTypedAccount()
if err != nil {
return errors.New("failed to get a valid user account").AtWarning().Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("failed to get a valid user account").AtWarning().Base(err)
}
account := rawAccount.(*ShadowsocksAccount)
request.User = user
Expand All @@ -95,7 +94,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
bufferedWriter := buf.NewBufferedWriter(conn)
bodyWriter, err := WriteTCPRequest(request, bufferedWriter)
if err != nil {
return errors.New("failed to write request").Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("failed to write request").Base(err)
}

if err := bufferedWriter.SetBuffered(false); err != nil {
Expand Down Expand Up @@ -126,7 +125,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
})

if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
return errors.New("connection ends").Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("connection ends").Base(err)
}

return nil
Expand All @@ -141,7 +140,7 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale

requestDone := signal.ExecuteAsync(func() error {
if err := buf.PipeUntilEOF(timer, outboundRay.OutboundInput(), writer); err != nil {
return errors.New("failed to transport all UDP request").Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("failed to transport all UDP request").Base(err)
}
return nil
})
Expand All @@ -155,13 +154,13 @@ func (v *Client) Process(ctx context.Context, outboundRay ray.OutboundRay, diale
}

if err := buf.PipeUntilEOF(timer, reader, outboundRay.OutboundOutput()); err != nil {
return errors.New("failed to transport all UDP response").Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("failed to transport all UDP response").Base(err)
}
return nil
})

if err := signal.ErrorOrFinish2(ctx, requestDone, responseDone); err != nil {
return errors.New("connection ends").Base(err).Path("Proxy", "Shadowsocks", "Client")
return newError("connection ends").Base(err)
}

return nil
Expand Down
7 changes: 7 additions & 0 deletions proxy/shadowsocks/errors.generated.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package shadowsocks

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

func newError(values ...interface{}) *errors.Error {
return errors.New(values...).Path("Proxy", "Shadowsocks")
}
2 changes: 2 additions & 0 deletions proxy/shadowsocks/shadowsocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@
//
// R.I.P Shadowsocks
package shadowsocks

//go:generate go run $GOPATH/src/v2ray.com/core/tools/generrorgen/main.go -pkg shadowsocks -path Proxy,Shadowsocks
44 changes: 44 additions & 0 deletions tools/generrorgen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"strings"
)

var (
pkg = flag.String("pkg", "", "Target package")
path = flag.String("path", "", "Path")
)

func main() {
flag.Parse()

if len(*pkg) == 0 {
panic("Package is not specified.")
}

if len(*path) == 0 {
panic("Path is not specified.")
}

paths := strings.Split(*path, ",")
for i := range paths {
paths[i] = "\"" + paths[i] + "\""
}
pathStr := strings.Join(paths, ", ")

file, err := os.OpenFile("errors.generated.go", os.O_WRONLY|os.O_TRUNC|os.O_CREATE, 0644)
if err != nil {
log.Fatalf("Failed to generate errors.generated.go: %v", err)
}
defer file.Close()

fmt.Fprintln(file, "package", *pkg)
fmt.Fprintln(file, "")
fmt.Fprintln(file, "import \"v2ray.com/core/common/errors\"")
fmt.Fprintln(file, "")
fmt.Fprintln(file, "func newError(values ...interface{}) *errors.Error { return errors.New(values...).Path("+pathStr+") }")
}

0 comments on commit 503f767

Please sign in to comment.