zodios는 ZodError를 ZodiosError.cause에 담아 던진다1 — instanceof z.ZodError로는 안 잡힌다.

if (err instanceof ZodiosError && err.cause instanceof ZodError) {
  console.log(fromZodError(err.cause).toString())
}

Footnotes

  1. zodios zod-validation.plugin.ts: 응답 검증 실패 시 throw new ZodiosError(… config, response.data, parsed.error) — 네 번째 인자가 cause다. ↩

#278

TS 를 ES5 로 내리면 Error 를 상속한 클래스의 instanceof 가 깨진다 — super() 직후 프로토타입을 직접 맞춘다. ES2015 이상 타깃이면 필요 없다.1

class CustomError extends Error {
  name = 'CustomError'

  constructor(message?: string) {
    super(message)

    Object.setPrototypeOf(this, CustomError.prototype)
  }
}

try {
  throw new CustomError('This is a custom error message.')
} catch (error) {
  if (error instanceof CustomError) {
    console.log('CustomError occurred:', error.message)
    console.log('Error name:', error.name)
  } else {
    console.log('An error occurred:', error)
  }
}

Footnotes

  1. TypeScript Breaking Changes — TypeScript 2.1: “constructor functions for Error, Array, and the like use ECMAScript 6’s new.target to adjust the prototype chain; however, there is no way to ensure a value for new.target when invoking a constructor in ECMAScript 5.” / “As a recommendation, you can manually adjust the prototype immediately after any super(...) calls.” ↩

#287

비동기 호출의 결과와 오류를 튜플 형식으로 반환

const [error, data] = await tuple(someAsyncFunction())
// 성공: [null, 결과값]
// 실패: [error] 또는 [new TupleItError(error)]

오류 처리 간소화 - 단일 체크로 오류 관리 가능 (Error 가 아닌 값으로 reject 돼도 TupleItError 로 감싸서 if (error) 하나로 갈린다)1


Footnotes

  1. tuple-it index.js: “Wrapping into TupleItError avoids the need to check if (error !== undefined) in favor of a simpler if (error)” ↩

#357


Footnotes

  1. 에러 메시지를 어디에·어떻게 보이나 의 디자인 지침 (Vitaly Friedman). 빨간색에만 기대지 않기, 자동 스크롤 금지, 입력값을 가리지 않기, 폼에선 입력란 위에·표에선 인라인으로, 토스트로 띄우지 않기, 사용자가 에러를 무시하고 진행할 수 있게, 올바른 입력 예시와 상단 요약. ↩

  2. Google Tech Writing 의 에러 메시지 문구 과정. 좋은 에러 메시지는 “무엇이 잘못됐나”와 “어떻게 고치나” 두 질문에 답한다 — 모호하거나 다음 행동을 알 수 없는 메시지가 지원 문의를 늘린다. GUI 에 띄우는 방식은 다루지 않는다. ↩

#429