Download this file
| 1 | package fixture |
| 2 | |
| 3 | // Not compiled. mapRefusal echoes a refusal only where its case tests the |
| 4 | // text against literals; the others do not. |
| 5 | |
| 6 | func refuse(w int, err error, other string) { |
| 7 | jsonError(w, 409, mapRefusal(err)) |
| 8 | jsonError(w, 409, unguarded(err)) |
| 9 | jsonError(w, 409, badGuard(err, other)) |
| 10 | } |
| 11 | |
| 12 | func mapRefusal(err error) string { |
| 13 | msg := err.Error() |
| 14 | switch { |
| 15 | case msg == "no journaled move in flight", strings.HasPrefix(msg, "a move to ") && strings.Contains(msg, " is already "): |
| 16 | return msg |
| 17 | } |
| 18 | return "this move's state does not allow that" |
| 19 | } |
| 20 | |
| 21 | func unguarded(err error) string { |
| 22 | msg := err.Error() |
| 23 | if len(msg) > 3 { |
| 24 | return msg |
| 25 | } |
| 26 | return "this move's state does not allow that" |
| 27 | } |
| 28 | |
| 29 | func badGuard(err error, other string) string { |
| 30 | msg := err.Error() |
| 31 | switch { |
| 32 | case msg == other: |
| 33 | return msg |
| 34 | } |
| 35 | return "this move's state does not allow that" |
| 36 | } |
| 37 | |