File size: 1,312 Bytes
4e23b01
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import { registerErrorDomain, type ErrorDomain } from '#/_base/errors/codes';
import { Error2, type Error2Options } from '#/_base/errors/errors';

export const StateErrors = {
  codes: {
    STATE_DUPLICATE_FOLD: 'state.duplicate_fold',
    STATE_DURABILITY_MISMATCH: 'state.durability_mismatch',
    STATE_CYCLE: 'state.cycle',
  },
  info: {
    'state.duplicate_fold': {
      title: 'Duplicate state fold',
      retryable: false,
      public: true,
      action: 'A state registered two folds for the same event; merge them.',
    },
    'state.durability_mismatch': {
      title: 'Transient state folds durable event',
      retryable: false,
      public: true,
      action: 'A non-durable state cannot fold a durable event; mark the state durable.',
    },
    'state.cycle': {
      title: 'Event dispatch cycle',
      retryable: false,
      public: true,
      action: 'A subscriber re-dispatches endlessly; break the event cycle.',
    },
  },
} as const satisfies ErrorDomain;

registerErrorDomain(StateErrors);

export type StateErrorCode = (typeof StateErrors.codes)[keyof typeof StateErrors.codes];

export class StateError extends Error2 {
  constructor(code: StateErrorCode, message: string, options?: Error2Options) {
    super(code, message, options);
    this.name = 'StateError';
  }
}