Browse Source

Tidied up error functions

Ryan Armstrong 4 years ago
parent
commit
d520734080
2 changed files with 23 additions and 14 deletions
  1. 3 3
      client.go
  2. 20 11
      error.go

+ 3 - 3
client.go

@@ -139,7 +139,7 @@ func (c *Client) do(req *Request) (*Response, error) {
 		if req.Size == 0 && hresp.ContentLength > 0 {
 			resp.Size = uint64(hresp.ContentLength)
 		} else if req.Size > 0 && hresp.ContentLength > 0 && req.Size != uint64(hresp.ContentLength) {
-			return resp, resp.close(errorf(errBadLength, "Bad content length: %d, expected %d", hresp.ContentLength, req.Size))
+			return resp, resp.close(newGrabError(errBadLength, "Bad content length: %d, expected %d", hresp.ContentLength, req.Size))
 		}
 
 		// does server supports resuming downloads?
@@ -157,7 +157,7 @@ func (c *Client) do(req *Request) (*Response, error) {
 	if needFilename {
 		filename := path.Base(req.HTTPRequest.URL.Path)
 		if filename == "" {
-			return resp, resp.close(errorf(errNoFilename, "No filename could be determined"))
+			return resp, resp.close(newGrabError(errNoFilename, "No filename could be determined"))
 		} else {
 			// update filepath with filename from URL
 			resp.Filename = filepath.Join(req.Filename, filename)
@@ -211,7 +211,7 @@ func (c *Client) do(req *Request) (*Response, error) {
 
 		// validate content length
 		if resp.Size > 0 && resp.Size != (resp.bytesTransferred+uint64(hresp.ContentLength)) {
-			return resp, resp.close(errorf(errBadLength, "Bad content length: %d, expected %d", hresp.ContentLength, resp.Size-resp.bytesTransferred))
+			return resp, resp.close(newGrabError(errBadLength, "Bad content length: %d, expected %d", hresp.ContentLength, resp.Size-resp.bytesTransferred))
 		}
 	}
 

+ 20 - 11
error.go

@@ -9,37 +9,46 @@ const (
 	errNoFilename
 )
 
+// grabError is a custom error type
 type grabError struct {
 	err  string
 	code int
 }
 
+// Error returns the error string of a grabError.
 func (c *grabError) Error() string {
 	return c.err
 }
 
-func errorf(code int, format string, a ...interface{}) error {
+// newGrabError creates a new grabError instance with the given code and
+// message.
+func newGrabError(code int, format string, a ...interface{}) error {
 	return &grabError{
 		err:  fmt.Sprintf(format, a...),
 		code: code,
 	}
 }
 
-// IsContentLengthMismatch returns a boolean indicating whether the error is
-// known to report that a HTTP request response indicated that the requested
-// file is not the expected length.
-func IsContentLengthMismatch(err error) bool {
+// isErrorType returns true if the given error is a grabError with the specified
+// error code.
+func isErrorType(err error, code int) bool {
 	if gerr, ok := err.(*grabError); ok {
-		return gerr.code == errBadLength
+		return gerr.code == code
 	}
 
 	return false
 }
 
-func IsNoFilename(err error) bool {
-	if gerr, ok := err.(*grabError); ok {
-		return gerr.code == errNoFilename
-	}
+// IsContentLengthMismatch returns a boolean indicating whether the error is
+// known to report that a HTTP request response indicated that the requested
+// file is not the expected length.
+func IsContentLengthMismatch(err error) bool {
+	return isErrorType(err, errBadLength)
+}
 
-	return false
+// IsNoFilename returns a boolean indicating whether the error is known to
+// report that a destination filename could not be determined from the
+// Content-Disposition headers of a HTTP response or the request URL's path.
+func IsNoFilename(err error) bool {
+	return isErrorType(err, errNoFilename)
 }