|
| 1 | +import { Schema } from "effect"; |
| 2 | + |
| 3 | +export class CliUsageError extends Schema.TaggedErrorClass<CliUsageError>()("CliUsageError", { |
| 4 | + message: Schema.String, |
| 5 | +}) {} |
| 6 | + |
| 7 | +export class ConfigError extends Schema.TaggedErrorClass<ConfigError>()("ConfigError", { |
| 8 | + message: Schema.String, |
| 9 | +}) {} |
| 10 | + |
| 11 | +export class ProcessExecutionError extends Schema.TaggedErrorClass<ProcessExecutionError>()( |
| 12 | + "ProcessExecutionError", |
| 13 | + { |
| 14 | + command: Schema.String, |
| 15 | + exitCode: Schema.Number, |
| 16 | + stdout: Schema.String, |
| 17 | + stderr: Schema.String, |
| 18 | + }, |
| 19 | +) {} |
| 20 | + |
| 21 | +export class ApiError extends Schema.TaggedErrorClass<ApiError>()("ApiError", { |
| 22 | + message: Schema.String, |
| 23 | + status: Schema.optional(Schema.Number), |
| 24 | + body: Schema.optional(Schema.String), |
| 25 | +}) {} |
| 26 | + |
| 27 | +export class HookBlockedError extends Schema.TaggedErrorClass<HookBlockedError>()( |
| 28 | + "HookBlockedError", |
| 29 | + { |
| 30 | + message: Schema.String, |
| 31 | + reason: Schema.optional(Schema.String), |
| 32 | + lastMessage: Schema.optional(Schema.String), |
| 33 | + }, |
| 34 | +) {} |
| 35 | + |
| 36 | +export class UnsupportedFeatureError extends Schema.TaggedErrorClass<UnsupportedFeatureError>()( |
| 37 | + "UnsupportedFeatureError", |
| 38 | + { |
| 39 | + message: Schema.String, |
| 40 | + }, |
| 41 | +) {} |
| 42 | + |
| 43 | +export const renderError = (error: unknown): string => { |
| 44 | + if (error instanceof CliUsageError) { |
| 45 | + return error.message; |
| 46 | + } |
| 47 | + if (error instanceof ConfigError) { |
| 48 | + return error.message; |
| 49 | + } |
| 50 | + if (error instanceof ApiError) { |
| 51 | + const parts = [error.message]; |
| 52 | + if (typeof error.status === "number") { |
| 53 | + parts.push(`status: ${error.status}`); |
| 54 | + } |
| 55 | + if (typeof error.body === "string" && error.body.trim().length > 0) { |
| 56 | + parts.push(error.body.trim()); |
| 57 | + } |
| 58 | + return parts.join("\n"); |
| 59 | + } |
| 60 | + if (error instanceof ProcessExecutionError) { |
| 61 | + const details = error.stderr.trim().length > 0 ? error.stderr.trim() : error.stdout.trim(); |
| 62 | + return `${error.command} exited with code ${error.exitCode}${details.length > 0 ? `\n${details}` : ""}`; |
| 63 | + } |
| 64 | + if (error instanceof HookBlockedError) { |
| 65 | + const lines = [error.message]; |
| 66 | + if (typeof error.reason === "string" && error.reason.trim().length > 0) { |
| 67 | + lines.push("", `hook rejected: ${error.reason.trim()}`); |
| 68 | + } |
| 69 | + if (typeof error.lastMessage === "string" && error.lastMessage.trim().length > 0) { |
| 70 | + lines.push("", "rejected message:", "", error.lastMessage.trim()); |
| 71 | + } |
| 72 | + return lines.join("\n"); |
| 73 | + } |
| 74 | + if (error instanceof UnsupportedFeatureError) { |
| 75 | + return error.message; |
| 76 | + } |
| 77 | + if (error instanceof Error) { |
| 78 | + return `${error.name}\n${error.message}`; |
| 79 | + } |
| 80 | + return String(error); |
| 81 | +}; |
0 commit comments