Skip to content

Commit

Permalink
StatusResponse: add String(). Helps providing detailed error info.
Browse files Browse the repository at this point in the history
  • Loading branch information
hsanjuan committed Oct 3, 2023
1 parent 47ddce8 commit dd200a1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion cmd/nopfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func main() {
fmt.Println(err)
} else {
status := blocker.IsCidBlocked(c)
fmt.Printf("%s: line %d. %s\n", status.Status, status.Entry.Line, status.Entry)
fmt.Println(status)
}
default:
fmt.Println("Usage:")
Expand Down
41 changes: 31 additions & 10 deletions status.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package nopfs

import (
"fmt"

"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
)
Expand Down Expand Up @@ -42,6 +44,35 @@ type StatusResponse struct {
Error error
}

// String provides a string with the details of a StatusResponse.
func (r StatusResponse) String() string {
if err := r.Error; err != nil {
return err.Error()
}

path := ""
if c := r.Cid; c.Defined() {
path = c.String()
} else {
path = r.Path.String()
}

return fmt.Sprintf("%s: %s (%s:%d)",
path, r.Status,
r.Filename, r.Entry.Line,
)
}

// ToError returns nil if the Status of the StatusResponse is Allowed or Not Found.
// When the status is Blocked or Errored, it returns a StatusError.
func (r StatusResponse) ToError() *StatusError {
if r.Status != StatusBlocked && r.Status != StatusErrored {
return nil
}

return &StatusError{Response: r}
}

// StatusError implements the error interface and can be used to provide
// information about a blocked-status in the form of an error.
type StatusError struct {
Expand All @@ -57,13 +88,3 @@ func (err *StatusError) Error() string {
}
return err.Response.Path.String() + " is blocked and cannot be provided"
}

// ToError returns nil if the Status of the StatusResponse is Allowed or Not Found.
// When the status is Blocked or Errored, it returns a StatusError.
func (r StatusResponse) ToError() *StatusError {
if r.Status != StatusBlocked && r.Status != StatusErrored {
return nil
}

return &StatusError{Response: r}
}

0 comments on commit dd200a1

Please sign in to comment.