gitee.com/sy_183/go-common@v1.0.5-0.20231205030221-958cfe129b47/lifecycle/errors.go (about)

     1  package lifecycle
     2  
     3  import (
     4  	"fmt"
     5  )
     6  
     7  type UnknownStateError struct {
     8  	Target string
     9  	State  State
    10  }
    11  
    12  func NewUnknownStateError(target string, state State) *UnknownStateError {
    13  	return &UnknownStateError{Target: target, State: state}
    14  }
    15  
    16  func (e *UnknownStateError) Error() string {
    17  	if e == nil {
    18  		return "<nil>"
    19  	}
    20  	if e.Target == "" {
    21  		return fmt.Sprintf("未知的生命周期组件状态(%s)", e.State)
    22  	} else {
    23  		return fmt.Sprintf("未知的%s状态(%s)", e.Target, e.State)
    24  	}
    25  }
    26  
    27  type StateDependencyError struct {
    28  	Target string
    29  	State  string
    30  	Depend string
    31  }
    32  
    33  func NewStateDependencyError(target string, state string, depend string) *StateDependencyError {
    34  	return &StateDependencyError{Target: target, State: state, Depend: depend}
    35  }
    36  
    37  func (e *StateDependencyError) Error() string {
    38  	if e == nil {
    39  		return "<nil>"
    40  	}
    41  	if e.Target == "" {
    42  		return fmt.Sprintf("生命周期组件状态(%s)依赖(%s)", e.State, e.Depend)
    43  	} else {
    44  		return fmt.Sprintf("%s状态(%s)依赖(%s)", e.Target, e.State, e.Depend)
    45  	}
    46  }
    47  
    48  type StateConflictError struct {
    49  	Target   string
    50  	State    string
    51  	Conflict string
    52  }
    53  
    54  func NewStateConflictError(target string, state string, conflict string) *StateConflictError {
    55  	return &StateConflictError{Target: target, State: state, Conflict: conflict}
    56  }
    57  
    58  func (e *StateConflictError) Error() string {
    59  	if e == nil {
    60  		return "<nil>"
    61  	}
    62  	if e.Target == "" {
    63  		return fmt.Sprintf("生命周期组件状态(%s)与(%s)冲突", e.State, e.Conflict)
    64  	} else {
    65  		return fmt.Sprintf("%s状态(%s)与(%s)冲突", e.Target, e.State, e.Conflict)
    66  	}
    67  }
    68  
    69  type StateNotAllowSwitchError struct {
    70  	Target string
    71  	Form   string
    72  	To     string
    73  }
    74  
    75  func NewStateNotAllowSwitchError(target string, from string, to string) *StateNotAllowSwitchError {
    76  	return &StateNotAllowSwitchError{Target: target, Form: from, To: to}
    77  }
    78  
    79  func (e *StateNotAllowSwitchError) Error() string {
    80  	if e == nil {
    81  		return "<nil>"
    82  	}
    83  	if e.Target == "" {
    84  		return fmt.Sprintf("生命周期组件不可以从(%s)转变到(%s)", e.Form, e.To)
    85  	} else {
    86  		return fmt.Sprintf("%s不可以从(%s)转变到(%s)", e.Target, e.Form, e.To)
    87  	}
    88  }
    89  
    90  type StateMustSwitchError struct {
    91  	Target string
    92  	Form   string
    93  	To     string
    94  }
    95  
    96  func NewStateMustSwitchError(target string, from string, to string) *StateMustSwitchError {
    97  	return &StateMustSwitchError{Target: target, Form: from, To: to}
    98  }
    99  
   100  func (e *StateMustSwitchError) Error() string {
   101  	if e == nil {
   102  		return "<nil>"
   103  	}
   104  	if e.Target == "" {
   105  		return fmt.Sprintf("生命周期组件必须从(%s)转变到(%s)", e.Form, e.To)
   106  	} else {
   107  		return fmt.Sprintf("%s必须从(%s)转变到(%s)", e.Target, e.Form, e.To)
   108  	}
   109  }
   110  
   111  type InterruptedError struct {
   112  	Target string
   113  	Action string
   114  }
   115  
   116  func NewInterruptedError(target string, action string) *InterruptedError {
   117  	return &InterruptedError{Target: target, Action: action}
   118  }
   119  
   120  func (e *InterruptedError) Error() string {
   121  	if e == nil {
   122  		return "<nil>"
   123  	}
   124  	if e.Target == "" {
   125  		if e.Action == "" {
   126  			return "生命周期组件已被中断"
   127  		} else {
   128  			return fmt.Sprintf("生命周期组件%s已被中断", e.Action)
   129  		}
   130  	} else {
   131  		return fmt.Sprintf("%s%s已被中断", e.Target, e.Action)
   132  	}
   133  }
   134  
   135  type StateStartingError struct {
   136  	Target string
   137  }
   138  
   139  func NewStateStartingError(target string) *StateStartingError {
   140  	return &StateStartingError{Target: target}
   141  }
   142  
   143  func (e *StateStartingError) Error() string {
   144  	if e == nil {
   145  		return "<nil>"
   146  	}
   147  	if e.Target == "" {
   148  		return "生命周期组件正在启动"
   149  	} else {
   150  		return fmt.Sprintf("%s正在启动", e.Target)
   151  	}
   152  }
   153  
   154  type StateRunningError struct {
   155  	Target string
   156  }
   157  
   158  func NewStateRunningError(target string) *StateRunningError {
   159  	return &StateRunningError{Target: target}
   160  }
   161  
   162  func (e *StateRunningError) Error() string {
   163  	if e == nil {
   164  		return "<nil>"
   165  	}
   166  	if e.Target == "" {
   167  		return "生命周期组件正在运行"
   168  	} else {
   169  		return fmt.Sprintf("%s正在运行", e.Target)
   170  	}
   171  }
   172  
   173  type StateNotRunningError struct {
   174  	Target string
   175  }
   176  
   177  func NewStateNotRunningError(target string) *StateNotRunningError {
   178  	return &StateNotRunningError{Target: target}
   179  }
   180  
   181  func (e *StateNotRunningError) Error() string {
   182  	if e == nil {
   183  		return "<nil>"
   184  	}
   185  	if e.Target == "" {
   186  		return "生命周期组件未运行"
   187  	} else {
   188  		return fmt.Sprintf("%s未运行", e.Target)
   189  	}
   190  }
   191  
   192  type StateClosingError struct {
   193  	Target string
   194  }
   195  
   196  func NewStateClosingError(target string) *StateClosingError {
   197  	return &StateClosingError{Target: target}
   198  }
   199  
   200  func (e *StateClosingError) Error() string {
   201  	if e == nil {
   202  		return "<nil>"
   203  	}
   204  	if e.Target == "" {
   205  		return "生命周期组件正在关闭"
   206  	} else {
   207  		return fmt.Sprintf("%s正在关闭", e.Target)
   208  	}
   209  }
   210  
   211  type StateClosedError struct {
   212  	Target string
   213  }
   214  
   215  func NewStateClosedError(target string) *StateClosedError {
   216  	return &StateClosedError{Target: target}
   217  }
   218  
   219  func (e *StateClosedError) Error() string {
   220  	if e == nil {
   221  		return "<nil>"
   222  	}
   223  	if e.Target == "" {
   224  		return "生命周期组件已经关闭"
   225  	} else {
   226  		return fmt.Sprintf("%s已经关闭", e.Target)
   227  	}
   228  }
   229  
   230  type StateNotClosedError struct {
   231  	Target string
   232  }
   233  
   234  func NewStateNotClosedError(target string) *StateNotClosedError {
   235  	return &StateNotClosedError{Target: target}
   236  }
   237  
   238  func (e *StateNotClosedError) Error() string {
   239  	if e == nil {
   240  		return "<nil>"
   241  	}
   242  	if e.Target == "" {
   243  		return "生命周期组件未关闭"
   244  	} else {
   245  		return fmt.Sprintf("%s未关闭", e.Target)
   246  	}
   247  }