MCPcopy Create free account
hub / github.com/CommandCodeAI/BaseAI / ApiErrorZod

Class ApiErrorZod

packages/baseai/src/dev/hono/errors.ts:139–183  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

137}
138
139export class ApiErrorZod extends HTTPException {
140 public readonly code: ErrorCodesType;
141
142 constructor({
143 code,
144 validationResult,
145 customMessage
146 }: {
147 code: ErrorCodesType;
148 validationResult: { success: boolean; error?: ZodError };
149 customMessage?: string;
150 }) {
151 let message = '';
152 if (validationResult.error) {
153 const _customMessage = customMessage ? customMessage : ``;
154 let _validationMessage = fromZodError(
155 validationResult.error
156 ).toString();
157 message = `${_customMessage} HINT: ${_validationMessage}`.trim();
158 }
159
160 super(codeToStatus(code), { message });
161 this.code = code;
162 this.cause = validationResult.error;
163 }
164
165 // Helper static method to simplify throwing errors
166 static handle({
167 code,
168 result,
169 message
170 }: {
171 code: ErrorCodesType;
172 result: ReturnType<AnyZodObject['safeParse']>;
173 message?: string;
174 }) {
175 if (!result.success) {
176 throw new ApiErrorZod({
177 code,
178 validationResult: result,
179 customMessage: message
180 });
181 }
182 }
183}
184
185/**
186 * Handles errors and returns a response object.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected