zodios는 ZodError를 ZodiosError.cause에 담아 던진다1 — instanceof z.ZodError로는 안 잡힌다.
if (err instanceof ZodiosError && err.cause instanceof ZodError) {
console.log(fromZodError(err.cause).toString())
}
- isErrorFromAlias not finding errors when uri have undefined params · Issue #402 · ecyrbe/zodios
- https://www.jacobparis.com/content/type-safe-env
Footnotes
-
zodios
zod-validation.plugin.ts: 응답 검증 실패 시throw new ZodiosError(… config, response.data, parsed.error)— 네 번째 인자가cause다. ↩
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)
}
}
- https://github.com/Alex-D/check-disk-space/blob/main/src/errors/invalidPathError.ts
- https://github.com/adriengibrat/ts-custom-error/blob/main/src/custom-error.ts
- 📘 타입스크립트 커스텀 Error 처리하기
- Handling errors like a pro in TypeScript | by Kolby Sisk | Udacity Eng & Data
Footnotes
-
TypeScript Breaking Changes — TypeScript 2.1: “constructor functions for
Error,Array, and the like use ECMAScript 6’snew.targetto adjust the prototype chain; however, there is no way to ensure a value fornew.targetwhen invoking a constructor in ECMAScript 5.” / “As a recommendation, you can manually adjust the prototype immediately after anysuper(...)calls.” ↩
- https://github.com/arthurfiorette/proposal-safe-assignment-operator
- https://github.com/arthurfiorette/tuple-it
비동기 호출의 결과와 오류를 튜플 형식으로 반환
const [error, data] = await tuple(someAsyncFunction())
// 성공: [null, 결과값]
// 실패: [error] 또는 [new TupleItError(error)]
오류 처리 간소화 - 단일 체크로 오류 관리 가능 (Error 가 아닌 값으로 reject 돼도 TupleItError 로 감싸서 if (error) 하나로 갈린다)1
Footnotes
-
tuple-it
index.js: “Wrapping into TupleItError avoids the need to checkif (error !== undefined)in favor of a simplerif (error)” ↩
- https://www.smashingmagazine.com/2022/08/error-messages-ux-design/ 1
- https://developers.google.com/tech-writing/error-messages 2
Footnotes
-
에러 메시지를 어디에·어떻게 보이나 의 디자인 지침 (Vitaly Friedman). 빨간색에만 기대지 않기, 자동 스크롤 금지, 입력값을 가리지 않기, 폼에선 입력란 위에·표에선 인라인으로, 토스트로 띄우지 않기, 사용자가 에러를 무시하고 진행할 수 있게, 올바른 입력 예시와 상단 요약. ↩
-
Google Tech Writing 의 에러 메시지 문구 과정. 좋은 에러 메시지는 “무엇이 잘못됐나”와 “어떻게 고치나” 두 질문에 답한다 — 모호하거나 다음 행동을 알 수 없는 메시지가 지원 문의를 늘린다. GUI 에 띄우는 방식은 다루지 않는다. ↩