Skip to content

Exception Handlers

ExceptionHandler는 Guard 실패 또는 핸들러 에러를 잡아 구조화된 응답으로 변환한다.

IpcExceptionHandler 인터페이스

ts
interface IpcExceptionHandler {
  catch(error: unknown, ctx: IpcContext): IpcError;
}

Guard가 false를 반환하거나 핸들러에서 에러가 발생하면 첫 번째 ExceptionHandler의 catch()가 호출된다. catch()IpcError ({ code: string; message: string })를 반환해야 한다. 프레임워크가 이를 IpcResponse{ ok: false, error } 형태로 렌더러에 전달한다.

handler가 없으면 프레임워크가 기본 IpcError를 생성한다.

커스텀 ExceptionHandler 작성

ts
import type { IpcExceptionHandler, IpcContext, IpcError } from '@repo/electron-ipc';

class ErrorHandler implements IpcExceptionHandler {
  catch(error: unknown, ctx: IpcContext): IpcError {
    const message = error instanceof Error ? error.message : 'Unknown error';
    return {
      code: 'APP_ERROR',
      message,
    };
  }
}

적용

ExceptionHandler는 createApp()exceptionHandlers 배열로 전달한다:

ts
createApp({
  guards: [new SenderGuard(), new AuthGuard()],
  exceptionHandlers: [new ErrorHandler()],
  modules: [WindowModule, MyModule],
});

다음 단계