Commit 1b301bac authored by Russ Cox's avatar Russ Cox

throw away os._Error.

make some error types in a few packages

R=r
DELTA=110  (25 added, 46 deleted, 39 changed)
OCL=28382
CL=28561
parent 01712ae7
...@@ -25,12 +25,16 @@ const ( ...@@ -25,12 +25,16 @@ const (
) )
// Errors introduced by this package. // Errors introduced by this package.
type Error struct {
os.ErrorString;
}
var ( var (
PhaseError = os.NewError("phase error"); PhaseError os.Error = &Error{"phase error"};
BufferFull = os.NewError("buffer full"); BufferFull os.Error = &Error{"buffer full"};
InternalError = os.NewError("bufio internal error"); InternalError os.Error = &Error{"bufio internal error"};
BadBufSize = os.NewError("bad bufio size"); BadBufSize os.Error = &Error{"bad bufio size"};
ShortWrite = os.NewError("short write"); ShortWrite os.Error = &Error{"short write"};
) )
func copySlice(dst []byte, src []byte) { func copySlice(dst []byte, src []byte) {
......
...@@ -26,13 +26,16 @@ const ( ...@@ -26,13 +26,16 @@ const (
) )
// HTTP request parsing errors. // HTTP request parsing errors.
type ProtocolError struct {
os.ErrorString
}
var ( var (
LineTooLong = os.NewError("http header line too long"); LineTooLong = &ProtocolError{"http header line too long"};
ValueTooLong = os.NewError("http header value too long"); ValueTooLong = &ProtocolError{"http header value too long"};
HeaderTooLong = os.NewError("http header too long"); HeaderTooLong = &ProtocolError{"http header too long"};
BadHeader = os.NewError("malformed http header"); BadHeader = &ProtocolError{"malformed http header"};
BadRequest = os.NewError("invalid http request"); BadRequest = &ProtocolError{"invalid http request"};
BadHTTPVersion = os.NewError("unsupported http version"); BadHTTPVersion = &ProtocolError{"unsupported http version"};
) )
// A Request represents a parsed HTTP request header. // A Request represents a parsed HTTP request header.
......
...@@ -13,9 +13,9 @@ import ( ...@@ -13,9 +13,9 @@ import (
) )
// Errors introduced by ParseURL. // Errors introduced by ParseURL.
var ( type BadURL struct {
BadURL = os.NewError("bad url syntax") os.ErrorString
) }
func ishex(c byte) bool { func ishex(c byte) bool {
switch { switch {
...@@ -52,7 +52,7 @@ func URLUnescape(s string) (string, os.Error) { ...@@ -52,7 +52,7 @@ func URLUnescape(s string) (string, os.Error) {
if s[i] == '%' { if s[i] == '%' {
n++; n++;
if !ishex(s[i+1]) || !ishex(s[i+2]) { if !ishex(s[i+1]) || !ishex(s[i+2]) {
return "", BadURL; return "", BadURL{"invalid hexadecimal escape"}
} }
i += 3 i += 3
} else { } else {
...@@ -110,7 +110,7 @@ func getscheme(rawurl string) (scheme, path string, err os.Error) { ...@@ -110,7 +110,7 @@ func getscheme(rawurl string) (scheme, path string, err os.Error) {
} }
case c == ':': case c == ':':
if i == 0 { if i == 0 {
return "", "", BadURL return "", "", BadURL{"missing protocol scheme"}
} }
return rawurl[0:i], rawurl[i+1:len(rawurl)], nil return rawurl[0:i], rawurl[i+1:len(rawurl)], nil
} }
...@@ -141,7 +141,7 @@ func split(s string, c byte, cutc bool) (string, string) { ...@@ -141,7 +141,7 @@ func split(s string, c byte, cutc bool) (string, string) {
// (Web browsers strip #fragment before sending the URL to a web server.) // (Web browsers strip #fragment before sending the URL to a web server.)
func ParseURL(rawurl string) (url *URL, err os.Error) { func ParseURL(rawurl string) (url *URL, err os.Error) {
if rawurl == "" { if rawurl == "" {
return nil, BadURL return nil, BadURL{"empty url"}
} }
url = new(URL); url = new(URL);
url.Raw = rawurl; url.Raw = rawurl;
......
...@@ -14,7 +14,10 @@ import ( ...@@ -14,7 +14,10 @@ import (
) )
// ErrEOF is the error returned by FullRead and Copyn when they encounter EOF. // ErrEOF is the error returned by FullRead and Copyn when they encounter EOF.
var ErrEOF = os.NewError("EOF") type Error struct {
os.ErrorString
}
var ErrEOF os.Error = &Error{"EOF"}
// Reader is the interface that wraps the basic Read method. // Reader is the interface that wraps the basic Read method.
type Reader interface { type Reader interface {
......
...@@ -41,16 +41,16 @@ coverage: packages ...@@ -41,16 +41,16 @@ coverage: packages
O1=\ O1=\
dnsmsg.$O\ dnsmsg.$O\
fd_$(GOOS).$O\
parse.$O\ parse.$O\
O2=\ O2=\
fd.$O\ fd_$(GOOS).$O\
ip.$O\ ip.$O\
port.$O\ port.$O\
O3=\ O3=\
dnsconfig.$O\ dnsconfig.$O\
fd.$O\
net_$(GOOS).$O\ net_$(GOOS).$O\
O4=\ O4=\
...@@ -64,15 +64,15 @@ phases: a1 a2 a3 a4 a5 ...@@ -64,15 +64,15 @@ phases: a1 a2 a3 a4 a5
_obj$D/net.a: phases _obj$D/net.a: phases
a1: $(O1) a1: $(O1)
$(AR) grc _obj$D/net.a dnsmsg.$O fd_$(GOOS).$O parse.$O $(AR) grc _obj$D/net.a dnsmsg.$O parse.$O
rm -f $(O1) rm -f $(O1)
a2: $(O2) a2: $(O2)
$(AR) grc _obj$D/net.a fd.$O ip.$O port.$O $(AR) grc _obj$D/net.a fd_$(GOOS).$O ip.$O port.$O
rm -f $(O2) rm -f $(O2)
a3: $(O3) a3: $(O3)
$(AR) grc _obj$D/net.a dnsconfig.$O net_$(GOOS).$O $(AR) grc _obj$D/net.a dnsconfig.$O fd.$O net_$(GOOS).$O
rm -f $(O3) rm -f $(O3)
a4: $(O4) a4: $(O4)
......
...@@ -26,17 +26,20 @@ import ( ...@@ -26,17 +26,20 @@ import (
) )
// DNS errors returned by LookupHost. // DNS errors returned by LookupHost.
type DNSError struct {
os.ErrorString
}
var ( var (
DNS_InternalError = os.NewError("internal dns error"); DNS_InternalError os.Error = &DNSError{"internal dns error"};
DNS_MissingConfig = os.NewError("no dns configuration"); DNS_MissingConfig os.Error = &DNSError{"no dns configuration"};
DNS_No_Answer = os.NewError("dns got no answer"); DNS_No_Answer os.Error = &DNSError{"dns got no answer"};
DNS_BadRequest = os.NewError("malformed dns request"); DNS_BadRequest os.Error = &DNSError{"malformed dns request"};
DNS_BadReply = os.NewError("malformed dns reply"); DNS_BadReply os.Error = &DNSError{"malformed dns reply"};
DNS_ServerFailure = os.NewError("dns server failure"); DNS_ServerFailure os.Error = &DNSError{"dns server failure"};
DNS_NoServers = os.NewError("no dns servers"); DNS_NoServers os.Error = &DNSError{"no dns servers"};
DNS_NameTooLong = os.NewError("dns name too long"); DNS_NameTooLong os.Error = &DNSError{"dns name too long"};
DNS_RedirectLoop = os.NewError("dns redirect loop"); DNS_RedirectLoop os.Error = &DNSError{"dns redirect loop"};
DNS_NameNotFound = os.NewError("dns name not found"); DNS_NameNotFound os.Error = &DNSError{"dns name not found"};
) )
// Send a request on the connection and hope for a reply. // Send a request on the connection and hope for a reply.
......
...@@ -12,6 +12,8 @@ import ( ...@@ -12,6 +12,8 @@ import (
"syscall"; "syscall";
) )
var kqueuePhaseError = &Error{"kqueue phase error"}
type pollster struct { type pollster struct {
kq int64; kq int64;
eventbuf [10]syscall.Kevent_t; eventbuf [10]syscall.Kevent_t;
...@@ -54,7 +56,7 @@ func (p *pollster) AddFD(fd int64, mode int, repeat bool) os.Error { ...@@ -54,7 +56,7 @@ func (p *pollster) AddFD(fd int64, mode int, repeat bool) os.Error {
return os.ErrnoToError(e) return os.ErrnoToError(e)
} }
if n != 1 || (ev.Flags & syscall.EV_ERROR) == 0 || ev.Ident != fd || ev.Filter != kmode { if n != 1 || (ev.Flags & syscall.EV_ERROR) == 0 || ev.Ident != fd || ev.Filter != kmode {
return os.NewError("kqueue phase error") return kqueuePhaseError
} }
if ev.Data != 0 { if ev.Data != 0 {
return os.ErrnoToError(ev.Data) return os.ErrnoToError(ev.Data)
......
...@@ -12,11 +12,11 @@ import ( ...@@ -12,11 +12,11 @@ import (
) )
var ( var (
BadAddress = os.NewError("malformed address"); BadAddress os.Error = &Error{"malformed address"};
MissingAddress = os.NewError("missing address"); MissingAddress os.Error = &Error{"missing address"};
UnknownNetwork = os.NewError("unknown network"); UnknownNetwork os.Error = &Error{"unknown network"};
UnknownHost = os.NewError("unknown host"); UnknownHost os.Error = &Error{"unknown host"};
UnknownSocketFamily = os.NewError("unknown socket family"); UnknownSocketFamily os.Error = &Error{"unknown socket family"};
) )
......
...@@ -12,6 +12,10 @@ import ( ...@@ -12,6 +12,10 @@ import (
"os"; "os";
) )
type Error struct {
os.ErrorString
}
type file struct { type file struct {
file *os.File; file *os.File;
data []byte; data []byte;
......
...@@ -16,7 +16,7 @@ import ( ...@@ -16,7 +16,7 @@ import (
// The error returned by LookupPort when a network service // The error returned by LookupPort when a network service
// is not listed in the database. // is not listed in the database.
var ErrNoService = os.NewError("unknown network service"); var ErrNoService = &Error{"unknown network service"};
var services map[string] map[string] int var services map[string] map[string] int
var servicesError os.Error var servicesError os.Error
......
...@@ -18,6 +18,11 @@ func (e ErrorString) String() string { ...@@ -18,6 +18,11 @@ func (e ErrorString) String() string {
return e return e
} }
// NewError converts s to an ErrorString, which satisfies the Error interface.
func NewError(s string) Error {
return ErrorString(s)
}
// Errno is the Unix error number. Names such as EINVAL are simple // Errno is the Unix error number. Names such as EINVAL are simple
// wrappers to convert the error number into an Error. // wrappers to convert the error number into an Error.
type Errno int64 type Errno int64
...@@ -74,48 +79,3 @@ var ( ...@@ -74,48 +79,3 @@ var (
ERANGE Error = Errno(syscall.ERANGE); ERANGE Error = Errno(syscall.ERANGE);
) )
// -----------------------
// Everything below here is deprecated.
// Delete when all callers of NewError are gone and their uses converted
// to the new error scheme (for an example, see template).
// _Error is a structure wrapping a string describing an error.
// Errors are singleton structures, created by NewError, so their addresses can
// be compared to test for equality. A nil Error pointer means ``no error''.
// Use the String() method to get the contents; it handles the nil case.
// The Error type is intended for use by any package that wishes to define
// error strings.
type _Error struct {
s string
}
// Table of all known errors in system. Use the same error string twice,
// get the same *os._Error.
var errorStringTab = make(map[string] Error);
// These functions contain a race if two goroutines add identical
// errors simultaneously but the consequences are unimportant.
// NewError allocates an Error object, but if s has been seen before,
// shares the _Error associated with that message.
func NewError(s string) Error {
if s == "" {
return nil
}
err, ok := errorStringTab[s];
if ok {
return err
}
err = &_Error{s};
errorStringTab[s] = err;
return err;
}
// String returns the string associated with the _Error.
func (e *_Error) String() string {
if e == nil {
return "No _Error"
}
return e.s
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment