Holger Hildebrandt | fa07499 | 2020-03-27 15:42:06 +0000 | [diff] [blame] | 1 | // Copyright (c) 2013 - Max Persson <max@looplab.se> |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package fsm |
| 16 | |
| 17 | // InvalidEventError is returned by FSM.Event() when the event cannot be called |
| 18 | // in the current state. |
| 19 | type InvalidEventError struct { |
| 20 | Event string |
| 21 | State string |
| 22 | } |
| 23 | |
| 24 | func (e InvalidEventError) Error() string { |
| 25 | return "event " + e.Event + " inappropriate in current state " + e.State |
| 26 | } |
| 27 | |
| 28 | // UnknownEventError is returned by FSM.Event() when the event is not defined. |
| 29 | type UnknownEventError struct { |
| 30 | Event string |
| 31 | } |
| 32 | |
| 33 | func (e UnknownEventError) Error() string { |
| 34 | return "event " + e.Event + " does not exist" |
| 35 | } |
| 36 | |
| 37 | // InTransitionError is returned by FSM.Event() when an asynchronous transition |
| 38 | // is already in progress. |
| 39 | type InTransitionError struct { |
| 40 | Event string |
| 41 | } |
| 42 | |
| 43 | func (e InTransitionError) Error() string { |
| 44 | return "event " + e.Event + " inappropriate because previous transition did not complete" |
| 45 | } |
| 46 | |
| 47 | // NotInTransitionError is returned by FSM.Transition() when an asynchronous |
| 48 | // transition is not in progress. |
| 49 | type NotInTransitionError struct{} |
| 50 | |
| 51 | func (e NotInTransitionError) Error() string { |
| 52 | return "transition inappropriate because no state change in progress" |
| 53 | } |
| 54 | |
| 55 | // NoTransitionError is returned by FSM.Event() when no transition have happened, |
| 56 | // for example if the source and destination states are the same. |
| 57 | type NoTransitionError struct { |
| 58 | Err error |
| 59 | } |
| 60 | |
| 61 | func (e NoTransitionError) Error() string { |
| 62 | if e.Err != nil { |
| 63 | return "no transition with error: " + e.Err.Error() |
| 64 | } |
| 65 | return "no transition" |
| 66 | } |
| 67 | |
| 68 | // CanceledError is returned by FSM.Event() when a callback have canceled a |
| 69 | // transition. |
| 70 | type CanceledError struct { |
| 71 | Err error |
| 72 | } |
| 73 | |
| 74 | func (e CanceledError) Error() string { |
| 75 | if e.Err != nil { |
| 76 | return "transition canceled with error: " + e.Err.Error() |
| 77 | } |
| 78 | return "transition canceled" |
| 79 | } |
| 80 | |
| 81 | // AsyncError is returned by FSM.Event() when a callback have initiated an |
| 82 | // asynchronous state transition. |
| 83 | type AsyncError struct { |
| 84 | Err error |
| 85 | } |
| 86 | |
| 87 | func (e AsyncError) Error() string { |
| 88 | if e.Err != nil { |
| 89 | return "async started with error: " + e.Err.Error() |
| 90 | } |
| 91 | return "async started" |
| 92 | } |
| 93 | |
| 94 | // InternalError is returned by FSM.Event() and should never occur. It is a |
| 95 | // probably because of a bug. |
| 96 | type InternalError struct{} |
| 97 | |
| 98 | func (e InternalError) Error() string { |
| 99 | return "internal error on state transition" |
| 100 | } |