| 1 | package fixture |
| 2 | |
| 3 | import "fmt" |
| 4 | |
| 5 | // Not compiled. Each sentence has text of its own, and a row with a |
| 6 | // <placeholder> would match it, but the placeholder carries an error's text. |
| 7 | |
| 8 | type refusal struct{} |
| 9 | |
| 10 | func (refusal) Error() string { return "refused" } |
| 11 | |
| 12 | type result struct{ Err error } |
| 13 | |
| 14 | // MV-N2's format with the error as its list. |
| 15 | func formatArg(w int, to string, err error) { |
| 16 | jsonError(w, 400, fmt.Sprintf("region %s is not one of this app's regions: %v", to, err)) |
| 17 | } |
| 18 | |
| 19 | func concatError(w int, err error) { |
| 20 | jsonError(w, 400, "the region refused it: "+err.Error()) |
| 21 | } |
| 22 | |
| 23 | // A %w argument is an error whatever its declared type. |
| 24 | func wrapVerb(w int, to string, why string) { |
| 25 | jsonError(w, 400, fmt.Sprintf("region %s failed: %w", to, why)) |
| 26 | } |
| 27 | |
| 28 | // A value of a type that implements error. |
| 29 | func implementsError(w int, r refusal) { |
| 30 | jsonError(w, 400, fmt.Sprintf("the region refused: %v", r)) |
| 31 | } |
| 32 | |
| 33 | // An identifier the gate cannot type, named like an error. |
| 34 | func unknownErr(w int) { |
| 35 | jsonError(w, 400, fmt.Sprintf("the last attempt failed: %v", lastErr)) |
| 36 | } |
| 37 | |
| 38 | // A field whose declared type is error. |
| 39 | func errorField(w int, res result) { |
| 40 | jsonError(w, 400, fmt.Sprintf("the copy failed: %s", res.Err)) |
| 41 | } |
| 42 | |
| 43 | // A local that holds an error's text. |
| 44 | func localText(w int, err error) { |
| 45 | detail := err.Error() |
| 46 | jsonError(w, 400, "the copy failed: "+detail) |
| 47 | } |
| 48 | |
| 49 | // A helper whose string parameter is bound to an error's text. |
| 50 | func viaHelper(w int, err error) { |
| 51 | jsonError(w, 400, explain(fmt.Sprint(err))) |
| 52 | } |
| 53 | |
| 54 | func explain(m string) string { |
| 55 | return "the copy failed: " + m |
| 56 | } |
| 57 | |