|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + "unicode/utf8" |
| 7 | + |
| 8 | + "github.com/ipfs/go-cid" |
| 9 | + ipld "github.com/ipfs/go-ipld-format" |
| 10 | + mbase "github.com/multiformats/go-multibase" |
| 11 | +) |
| 12 | + |
| 13 | +// This file handle parsing and returning the correct ABI based errors from error messages |
| 14 | + |
| 15 | +type prePostWrappedNotFoundError struct { |
| 16 | + pre string |
| 17 | + post string |
| 18 | + |
| 19 | + wrapped ipld.ErrNotFound |
| 20 | +} |
| 21 | + |
| 22 | +func (e prePostWrappedNotFoundError) String() string { |
| 23 | + return e.Error() |
| 24 | +} |
| 25 | + |
| 26 | +func (e prePostWrappedNotFoundError) Error() string { |
| 27 | + return e.pre + e.wrapped.Error() + e.post |
| 28 | +} |
| 29 | + |
| 30 | +func (e prePostWrappedNotFoundError) Unwrap() error { |
| 31 | + return e.wrapped |
| 32 | +} |
| 33 | + |
| 34 | +func parseErrNotFoundWithFallbackToMSG(msg string) error { |
| 35 | + err, handled := parseErrNotFound(msg) |
| 36 | + if handled { |
| 37 | + return err |
| 38 | + } |
| 39 | + |
| 40 | + return errors.New(msg) |
| 41 | +} |
| 42 | + |
| 43 | +func parseErrNotFoundWithFallbackToError(msg error) error { |
| 44 | + err, handled := parseErrNotFound(msg.Error()) |
| 45 | + if handled { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + return msg |
| 50 | +} |
| 51 | + |
| 52 | +//lint:ignore ST1008 this function is not using the error as a mean to return failure but it massages it to return the correct type |
| 53 | +func parseErrNotFound(msg string) (error, bool) { |
| 54 | + if msg == "" { |
| 55 | + return nil, true // Fast path |
| 56 | + } |
| 57 | + |
| 58 | + if err, handled := parseIPLDErrNotFound(msg); handled { |
| 59 | + return err, true |
| 60 | + } |
| 61 | + |
| 62 | + if err, handled := parseBlockstoreNotFound(msg); handled { |
| 63 | + return err, true |
| 64 | + } |
| 65 | + |
| 66 | + return nil, false |
| 67 | +} |
| 68 | + |
| 69 | +// Assume CIDs break on: |
| 70 | +// - Whitespaces: " \t\n\r\v\f" |
| 71 | +// - Semicolon: ";" this is to parse ipld.ErrNotFound wrapped in multierr |
| 72 | +// - Double Quotes: "\"" this is for parsing %q and %#v formating |
| 73 | +const cidBreakSet = " \t\n\r\v\f;\"" |
| 74 | + |
| 75 | +//lint:ignore ST1008 using error as values |
| 76 | +func parseIPLDErrNotFound(msg string) (error, bool) { |
| 77 | + // The patern we search for is: |
| 78 | + const ipldErrNotFoundKey = "ipld: could not find " /*CID*/ |
| 79 | + // We try to parse the CID, if it's invalid we give up and return a simple text error. |
| 80 | + // We also accept "node" in place of the CID because that means it's an Undefined CID. |
| 81 | + |
| 82 | + keyIndex := strings.Index(msg, ipldErrNotFoundKey) |
| 83 | + |
| 84 | + if keyIndex < 0 { // Unknown error |
| 85 | + return nil, false |
| 86 | + } |
| 87 | + |
| 88 | + cidStart := keyIndex + len(ipldErrNotFoundKey) |
| 89 | + |
| 90 | + msgPostKey := msg[cidStart:] |
| 91 | + var c cid.Cid |
| 92 | + var postIndex int |
| 93 | + if strings.HasPrefix(msgPostKey, "node") { |
| 94 | + // Fallback case |
| 95 | + c = cid.Undef |
| 96 | + postIndex = len("node") |
| 97 | + } else { |
| 98 | + postIndex = strings.IndexFunc(msgPostKey, func(r rune) bool { |
| 99 | + return strings.ContainsAny(string(r), cidBreakSet) |
| 100 | + }) |
| 101 | + if postIndex < 0 { |
| 102 | + // no breakage meaning the string look like this something + "ipld: could not find bafy" |
| 103 | + postIndex = len(msgPostKey) |
| 104 | + } |
| 105 | + |
| 106 | + cidStr := msgPostKey[:postIndex] |
| 107 | + |
| 108 | + var err error |
| 109 | + c, err = cid.Decode(cidStr) |
| 110 | + if err != nil { |
| 111 | + // failed to decode CID give up |
| 112 | + return nil, false |
| 113 | + } |
| 114 | + |
| 115 | + // check that the CID is either a CIDv0 or a base32 multibase |
| 116 | + // because that what ipld.ErrNotFound.Error() -> cid.Cid.String() do currently |
| 117 | + if c.Version() != 0 { |
| 118 | + baseRune, _ := utf8.DecodeRuneInString(cidStr) |
| 119 | + if baseRune == utf8.RuneError || baseRune != mbase.Base32 { |
| 120 | + // not a multibase we expect, give up |
| 121 | + return nil, false |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + err := ipld.ErrNotFound{Cid: c} |
| 127 | + pre := msg[:keyIndex] |
| 128 | + post := msgPostKey[postIndex:] |
| 129 | + |
| 130 | + if len(pre) > 0 || len(post) > 0 { |
| 131 | + return prePostWrappedNotFoundError{ |
| 132 | + pre: pre, |
| 133 | + post: post, |
| 134 | + wrapped: err, |
| 135 | + }, true |
| 136 | + } |
| 137 | + |
| 138 | + return err, true |
| 139 | +} |
| 140 | + |
| 141 | +// This is a simple error type that just return msg as Error(). |
| 142 | +// But that also match ipld.ErrNotFound when called with Is(err). |
| 143 | +// That is needed to keep compatiblity with code that use string.Contains(err.Error(), "blockstore: block not found") |
| 144 | +// and code using ipld.ErrNotFound |
| 145 | +type blockstoreNotFoundMatchingIPLDErrNotFound struct { |
| 146 | + msg string |
| 147 | +} |
| 148 | + |
| 149 | +func (e blockstoreNotFoundMatchingIPLDErrNotFound) String() string { |
| 150 | + return e.Error() |
| 151 | +} |
| 152 | + |
| 153 | +func (e blockstoreNotFoundMatchingIPLDErrNotFound) Error() string { |
| 154 | + return e.msg |
| 155 | +} |
| 156 | + |
| 157 | +func (e blockstoreNotFoundMatchingIPLDErrNotFound) Is(err error) bool { |
| 158 | + _, ok := err.(ipld.ErrNotFound) |
| 159 | + return ok |
| 160 | +} |
| 161 | + |
| 162 | +//lint:ignore ST1008 using error as values |
| 163 | +func parseBlockstoreNotFound(msg string) (error, bool) { |
| 164 | + if !strings.Contains(msg, "blockstore: block not found") { |
| 165 | + return nil, false |
| 166 | + } |
| 167 | + |
| 168 | + return blockstoreNotFoundMatchingIPLDErrNotFound{msg: msg}, true |
| 169 | +} |
0 commit comments