| 1 | package attachment |
| 2 | |
| 3 | import ( |
| 4 | "errors" |
| 5 | "fmt" |
| 6 | "strings" |
| 7 | ) |
| 8 | |
| 9 | type Code string |
| 10 | |
| 11 | const ( |
| 12 | CodeMissing Code = "missing" |
| 13 | CodeUnreadable Code = "unreadable" |
| 14 | CodeUnsafe Code = "unsafe_path" |
| 15 | CodeUnsupported Code = "unsupported_format" |
| 16 | CodeCorrupt Code = "corrupt" |
| 17 | CodeSize Code = "size_limit" |
| 18 | CodeChanged Code = "changed" |
| 19 | CodeCanceled Code = "canceled" |
| 20 | CodeTooMany Code = "too_many" |
| 21 | CodeBatchSize Code = "batch_size" |
| 22 | ) |
| 23 | |
| 24 | // Error is safe to return across UI/RPC: Name is a display name, not a host path. |
| 25 | type Error struct { |
| 26 | Code Code |
| 27 | Name string |
| 28 | Index int |
| 29 | Retry bool |
| 30 | Cause error |
| 31 | Message string |
| 32 | Diagnose string |
| 33 | } |
| 34 | |
| 35 | func (e Error) Error() string { |
| 36 | name := strings.TrimSpace(e.Name) |
| 37 | if name == "" { |
| 38 | name = "image" |
| 39 | } |
| 40 | detail := strings.TrimSpace(e.Message) |
| 41 | if detail == "" { |
| 42 | detail = defaultDetail(e.Code) |
| 43 | } |
| 44 | if e.Index > 0 { |
| 45 | return fmt.Sprintf("image attachment %d %q %s (%s)", e.Index, name, detail, e.Code) |
| 46 | } |
| 47 | return fmt.Sprintf("image attachment %q %s (%s)", name, detail, e.Code) |
| 48 | } |
| 49 | |
| 50 | func (e Error) Unwrap() error { return e.Cause } |
| 51 | |
| 52 | func (e Error) Retryable() bool { return e.Retry } |
| 53 | |
| 54 | type BatchError []Error |
| 55 | |
| 56 | func (e BatchError) Error() string { |
| 57 | if len(e) == 0 { |
| 58 | return "image attachments could not be read" |
| 59 | } |
| 60 | parts := make([]string, 0, len(e)) |
| 61 | for _, item := range e { |
| 62 | parts = append(parts, item.Error()) |
| 63 | } |
| 64 | return strings.Join(parts, "; ") |
| 65 | } |
| 66 | |
| 67 | func Is(err error, code Code) bool { |
| 68 | var item Error |
| 69 | if errors.As(err, &item) { |
| 70 | return item.Code == code |
| 71 | } |
| 72 | var batch BatchError |
| 73 | if errors.As(err, &batch) { |
| 74 | for _, item := range batch { |
| 75 | if item.Code == code { |
| 76 | return true |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | return false |
| 81 | } |
| 82 | |
| 83 | func canceledError(err error) Error { |
| 84 | return Error{Code: CodeCanceled, Message: "was canceled", Cause: err, Retry: true} |
| 85 | } |
| 86 | |
| 87 | func defaultDetail(code Code) string { |
| 88 | switch code { |
| 89 | case CodeMissing: |
| 90 | return "does not exist" |
| 91 | case CodeUnreadable: |
| 92 | return "could not be read" |
| 93 | case CodeUnsafe: |
| 94 | return "has an unsafe path" |
| 95 | case CodeUnsupported: |
| 96 | return "is not an image or uses an unsupported format" |
| 97 | case CodeCorrupt: |
| 98 | return "is damaged" |
| 99 | case CodeSize: |
| 100 | return "exceeds the allowed size" |
| 101 | case CodeChanged: |
| 102 | return "changed while it was being read" |
| 103 | case CodeCanceled: |
| 104 | return "was canceled" |
| 105 | case CodeTooMany: |
| 106 | return "exceeds the allowed count" |
| 107 | case CodeBatchSize: |
| 108 | return "exceeds the allowed batch size" |
| 109 | default: |
| 110 | return "could not be read" |
| 111 | } |
| 112 | } |
| 113 |