Skip to content
This repository has been archived by the owner on Jul 31, 2024. It is now read-only.

Commit

Permalink
Merge pull request #27 from rogpeppe/028-errgo
Browse files Browse the repository at this point in the history
all: use gopkg.in/errgo.v1
  • Loading branch information
rogpeppe committed Dec 7, 2014
2 parents 360336b + ec34d30 commit 4a8b59b
Show file tree
Hide file tree
Showing 20 changed files with 195 additions and 196 deletions.
20 changes: 10 additions & 10 deletions charmbits/httprelation/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ package httprelation
import (
"strconv"

"gopkg.in/errgo.v1"
"gopkg.in/juju/charm.v4"
"launchpad.net/errgo/errors"

"github.com/juju/gocharm/charmbits/simplerelation"
"github.com/juju/gocharm/hook"
Expand Down Expand Up @@ -83,13 +83,13 @@ func (p *Provider) HTTPSPort() int {
func (p *Provider) configChanged() error {

if err := p.configurePort(&p.state.OpenedHTTPPort, "http-port"); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
if p.allowHTTPS {
// If the TLSCert is invalid, ignore it.
if _, err := p.TLSCertPEM(); err == nil {
if err := p.configurePort(&p.state.OpenedHTTPSPort, "https-port"); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
}
}
Expand All @@ -101,18 +101,18 @@ func (p *Provider) configChanged() error {
}
addr, err := p.ctxt.PrivateAddress()
if err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
if err := p.prov.SetValues(map[string]string{
"hostname": addr,
"port": strconv.Itoa(p.state.OpenedHTTPPort),
}); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
return nil
}

var ErrHTTPSNotConfigured = errors.New("HTTPS not configured")
var ErrHTTPSNotConfigured = errgo.New("HTTPS not configured")

// TLSCertPEM returns the currently configured server certificate
// in PEM format. It returns ErrHTTPSNotConfigured if there is no currently
Expand All @@ -123,15 +123,15 @@ func (p *Provider) TLSCertPEM() (string, error) {
}
certPEM, err := p.ctxt.GetConfigString("https-certificate")
if err != nil {
return "", errors.Wrap(err)
return "", errgo.Mask(err)
}
return certPEM, nil
}

func (p *Provider) configurePort(openedPort *int, configKey string) error {
port, err := p.ctxt.GetConfigInt(configKey)
if err != nil {
return errors.Wrapf(err, "cannot get %s", configKey)
return errgo.Notef(err, "cannot get %s", configKey)
}
if port <= 0 || port >= 65535 {
p.ctxt.Logf("ignoring invalid %s %v", configKey, port)
Expand All @@ -145,12 +145,12 @@ func (p *Provider) configurePort(openedPort *int, configKey string) error {
// Could check actually opened ports here to be
// more resilient against previous errors.
if err := p.ctxt.ClosePort("tcp", *openedPort); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
*openedPort = 0
}
if err := p.ctxt.OpenPort("tcp", port); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
*openedPort = port
return nil
Expand Down
2 changes: 1 addition & 1 deletion charmbits/httprelation/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ package httprelation_test

import (
"encoding/json"
gc "gopkg.in/check.v1"
"sort"
"testing"

jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/juju/charm.v4"

"github.com/juju/gocharm/charmbits/httprelation"
Expand Down
60 changes: 30 additions & 30 deletions charmbits/httpservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"strconv"
"time"

"launchpad.net/errgo/errors"
"gopkg.in/errgo.v1"

"github.com/juju/gocharm/charmbits/httprelation"
"github.com/juju/gocharm/charmbits/service"
Expand Down Expand Up @@ -57,7 +57,7 @@ type localState struct {
func (svc *Service) Register(r *hook.Registry, serviceName, relationName string, handler interface{}) {
h, err := newHandler(handler)
if err != nil {
panic(errors.Wrapf(err, "cannot register handler function"))
panic(errgo.Notef(err, "cannot register handler function"))
}
svc.handler = h
svc.svc.Register(r.Clone("service"), serviceName, svc.startServer)
Expand All @@ -81,7 +81,7 @@ func (svc *Service) changed() error {
return svc.svc.Stop()
}
if err := svc.start(svc.state.StartArg); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
return nil
}
Expand All @@ -96,10 +96,10 @@ func (svc *Service) changed() error {
func (svc *Service) Start(arg interface{}) error {
argStr, err := svc.handler.marshal(arg)
if err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
if err := svc.start(argStr); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
return nil
}
Expand All @@ -110,11 +110,11 @@ func (svc *Service) Start(arg interface{}) error {
func (svc *Service) PublicHTTPURL() (string, error) {
addr, err := svc.ctxt.PublicAddress()
if err != nil {
return "", errors.Wrapf(err, "cannot get public address")
return "", errgo.Notef(err, "cannot get public address")
}
port := svc.http.HTTPPort()
if port == 0 {
return "", errors.New("port not currently set")
return "", errgo.New("port not currently set")
}
url := "http://" + addr
if port != 80 {
Expand All @@ -123,27 +123,27 @@ func (svc *Service) PublicHTTPURL() (string, error) {
return url, nil
}

var ErrHTTPSNotConfigured = errors.New("HTTPS not configured")
var ErrHTTPSNotConfigured = errgo.New("HTTPS not configured")

// PublicHTTPSURL returns an http URL that can be
// used to access the HTTPS service, not including
// the trailing slash. It returns ErrHTTPSNotConfigured
// if there is no current https service.
func (svc *Service) PublicHTTPSURL() (string, error) {
_, err := svc.http.TLSCertPEM()
if errors.Cause(err) == httprelation.ErrHTTPSNotConfigured {
if errgo.Cause(err) == httprelation.ErrHTTPSNotConfigured {
return "", ErrHTTPSNotConfigured
}
if err != nil {
return "", errors.Wrap(err)
return "", errgo.Mask(err)
}
port := svc.http.HTTPSPort()
if port == 0 {
return "", ErrHTTPSNotConfigured
}
addr, err := svc.ctxt.PublicAddress()
if err != nil {
return "", errors.Wrapf(err, "cannot get public address")
return "", errgo.Notef(err, "cannot get public address")
}
url := "https://" + addr
if port != 443 {
Expand All @@ -161,11 +161,11 @@ func (svc *Service) start(argStr string) error {
return nil
}
cert, err := svc.http.TLSCertPEM()
if err != nil && errors.Cause(err) != httprelation.ErrHTTPSNotConfigured {
return errors.Wrap(err)
if err != nil && errgo.Cause(err) != httprelation.ErrHTTPSNotConfigured {
return errgo.Mask(err)
}
if err := svc.svc.Start(strconv.Itoa(httpPort), strconv.Itoa(httpsPort), cert, argStr); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
svc.state.StartArg = argStr
return nil
Expand All @@ -174,7 +174,7 @@ func (svc *Service) start(argStr string) error {
// Stop stops the service.
func (svc *Service) Stop() error {
if err := svc.svc.Stop(); err != nil {
return errors.Wrap(err)
return errgo.Mask(err)
}
svc.state.Started = false
return nil
Expand All @@ -201,20 +201,20 @@ func (svc *Service) startServer(ctxt *service.Context, args []string) {

func (srv *server) start(ctxt *service.Context, args []string) error {
if len(args) != 4 {
return errors.Newf("got %d arguments, expected 2", len(args))
return errgo.Newf("got %d arguments, expected 2", len(args))
}
httpPort, err := strconv.Atoi(args[0])
if err != nil {
return errors.Newf("invalid port %q", args[0])
return errgo.Newf("invalid port %q", args[0])
}
httpsPort, err := strconv.Atoi(args[1])
if err != nil {
return errors.Newf("invalid port %q", args[1])
return errgo.Newf("invalid port %q", args[1])
}
certPEM := args[2]
h, err := srv.handler.get(args[3])
if err != nil {
return errors.Newf("cannot get handler: %v", err)
return errgo.Newf("cannot get handler: %v", err)
}
done := make(chan error, 2)
if httpPort != 0 {
Expand All @@ -227,14 +227,14 @@ func (srv *server) start(ctxt *service.Context, args []string) error {
done <- srv.serveHTTPS(httpsPort, certPEM, h)
}()
}
return errors.Wrap(<-done)
return errgo.Mask(<-done)
}

func (*server) serveHTTP(port int, h http.Handler) error {
addr := ":" + strconv.Itoa(port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return errors.Newf("cannot listen on %s: %v", addr, err)
return errgo.Newf("cannot listen on %s: %v", addr, err)
}
server := &http.Server{
Addr: addr,
Expand All @@ -247,7 +247,7 @@ func (*server) serveHTTPS(port int, certPEM string, h http.Handler) error {
certPEMBytes := []byte(certPEM)
cert, err := tls.X509KeyPair(certPEMBytes, certPEMBytes)
if err != nil {
return errors.Newf("cannot parse certificate: %v", err)
return errgo.Newf("cannot parse certificate: %v", err)
}
config := &tls.Config{
NextProtos: []string{"http/1.1"},
Expand All @@ -256,7 +256,7 @@ func (*server) serveHTTPS(port int, certPEM string, h http.Handler) error {
addr := ":" + strconv.Itoa(port)
listener, err := net.Listen("tcp", addr)
if err != nil {
return errors.Newf("cannot listen on %s: %v", addr, err)
return errgo.Newf("cannot listen on %s: %v", addr, err)
}
tlsListener := tls.NewListener(
tcpKeepAliveListener{listener.(*net.TCPListener)},
Expand All @@ -283,16 +283,16 @@ func newHandler(f interface{}) (*handler, error) {
fv := reflect.ValueOf(f)
ft := fv.Type()
if ft.Kind() != reflect.Func {
return nil, errors.Newf("bad handler; got %T, expected function", f)
return nil, errgo.Newf("bad handler; got %T, expected function", f)
}
if n := ft.NumIn(); n != 1 {
return nil, errors.Newf("bad handler; got %d arguments, expected 1", n)
return nil, errgo.Newf("bad handler; got %d arguments, expected 1", n)
}
if n := ft.NumOut(); n != 2 {
return nil, errors.Newf("bad handler; got %d return values, expected 2", n)
return nil, errgo.Newf("bad handler; got %d return values, expected 2", n)
}
if ft.Out(0) != httpHandlerType || ft.Out(1) != errorType {
return nil, errors.Newf("bad handler; got return values (%s, %s), expected (http.Handler, error)", ft.Out(0), ft.Out(1))
return nil, errgo.Newf("bad handler; got return values (%s, %s), expected (http.Handler, error)", ft.Out(0), ft.Out(1))
}
return &handler{
argType: ft.In(0),
Expand All @@ -304,7 +304,7 @@ func (h *handler) get(arg string) (http.Handler, error) {
argv := reflect.New(h.argType)
err := json.Unmarshal([]byte(arg), argv.Interface())
if err != nil {
return nil, errors.Wrapf(err, "cannot unmarshal into %s", argv.Type())
return nil, errgo.Notef(err, "cannot unmarshal into %s", argv.Type())
}
r := h.fv.Call([]reflect.Value{argv.Elem()})
if err := r[1].Interface(); err != nil {
Expand All @@ -316,11 +316,11 @@ func (h *handler) get(arg string) (http.Handler, error) {
func (h *handler) marshal(arg interface{}) (string, error) {
argv := reflect.ValueOf(arg)
if argv.Type() != h.argType {
return "", errors.Newf("unexpected argument type; got %s, expected %s", argv.Type(), h.argType)
return "", errgo.Newf("unexpected argument type; got %s, expected %s", argv.Type(), h.argType)
}
data, err := json.Marshal(argv.Interface())
if err != nil {
return "", errors.Wrapf(err, "cannot marshal %#v", arg)
return "", errgo.Notef(err, "cannot marshal %#v", arg)
}
return string(data), nil
}
Expand Down
4 changes: 2 additions & 2 deletions charmbits/mongodbrelation/requirer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net"
"strings"

"launchpad.net/errgo/errors"
"gopkg.in/errgo.v1"

"github.com/juju/gocharm/charmbits/simplerelation"
"github.com/juju/gocharm/hook"
Expand Down Expand Up @@ -49,7 +49,7 @@ func unitAddress(vals map[string]string) (string, error) {
}
port := vals["port"]
if port == "" {
return "", errors.Newf("mongodb host %q found with no port", host)
return "", errgo.Newf("mongodb host %q found with no port", host)
}
return net.JoinHostPort(host, port), nil
}
8 changes: 4 additions & 4 deletions charmbits/service/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"net/rpc/jsonrpc"
"os"

"launchpad.net/errgo/errors"
"gopkg.in/errgo.v1"
)

type serviceParams struct {
Expand Down Expand Up @@ -61,13 +61,13 @@ func (ctxt *Context) ServeLocalRPC(rcvr interface{}) error {
srv.Register(rcvr)
listener, err := listen(ctxt.socketPath)
if err != nil {
return errors.Wrapf(err, "cannot listen on local socket")
return errgo.Notef(err, "cannot listen on local socket")
}
for {
log.Printf("accepting local service on %s", ctxt.socketPath)
conn, err := listener.Accept()
if err != nil {
return errors.Wrapf(err, "local socket accept failed")
return errgo.Notef(err, "local socket accept failed")
}
log.Printf("accepted dial request")
go srv.ServeCodec(jsonrpc.NewServerCodec(conn))
Expand All @@ -79,7 +79,7 @@ func listen(socketPath string) (net.Listener, error) {
os.Remove(socketPath)
listener, err := net.Listen("unix", socketPath)
if err != nil {
return nil, errors.Wrapf(err, "cannot listen on unix socket")
return nil, errgo.Notef(err, "cannot listen on unix socket")
}
return listener, err
}
Loading

0 comments on commit 4a8b59b

Please sign in to comment.