diff options
| author | Dax Raad <[email protected]> | 2025-06-10 10:10:45 -0400 |
|---|---|---|
| committer | Dax Raad <[email protected]> | 2025-06-10 10:14:03 -0400 |
| commit | 34fa8cadd6318f606d676dc7476bee1c97c40be2 (patch) | |
| tree | 7c7f5420e929e9b765a3037bbc978001a4e0d4f1 | |
| parent | edd459ec00a0c293052ad7d2dfedd90ffcc7e350 (diff) | |
| download | opencode-34fa8cadd6318f606d676dc7476bee1c97c40be2.tar.gz opencode-34fa8cadd6318f606d676dc7476bee1c97c40be2.zip | |
Improve ripgrep error handling with structured error types
🤖 Generated with [OpenCode](https://opencode.ai)
Co-Authored-By: OpenCode <[email protected]>
| -rw-r--r-- | packages/opencode/src/ripgrep/index.ts | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/packages/opencode/src/ripgrep/index.ts b/packages/opencode/src/ripgrep/index.ts index 01f486631..9c0646976 100644 --- a/packages/opencode/src/ripgrep/index.ts +++ b/packages/opencode/src/ripgrep/index.ts @@ -2,6 +2,8 @@ import { App } from "../app/app" import path from "path" import { Global } from "../global" import fs from "fs/promises" +import { z } from "zod" +import { NamedError } from "../util/error" export namespace Ripgrep { const PLATFORM = { @@ -10,6 +12,29 @@ export namespace Ripgrep { win32: { platform: "pc-windows-msvc", extension: "zip" }, } as const + export const ExtractionFailedError = NamedError.create( + "RipgrepExtractionFailedError", + z.object({ + filepath: z.string(), + stderr: z.string(), + }), + ) + + export const UnsupportedPlatformError = NamedError.create( + "RipgrepUnsupportedPlatformError", + z.object({ + platform: z.string(), + }), + ) + + export const DownloadFailedError = NamedError.create( + "RipgrepDownloadFailedError", + z.object({ + url: z.string(), + status: z.number(), + }), + ) + const state = App.state("ripgrep", async () => { const filepath = path.join( Global.Path.bin, @@ -22,7 +47,8 @@ export namespace Ripgrep { const arch = archMap[process.arch as keyof typeof archMap] ?? process.arch const config = PLATFORM[process.platform as keyof typeof PLATFORM] - if (!config) throw new Error(`Unsupported platform: ${process.platform}`) + if (!config) + throw new UnsupportedPlatformError({ platform: process.platform }) const version = "14.1.1" const filename = `ripgrep-${version}-${arch}-${config.platform}.${config.extension}` @@ -30,7 +56,7 @@ export namespace Ripgrep { const response = await fetch(url) if (!response.ok) - throw new Error(`Failed to download ripgrep: ${response.statusText}`) + throw new DownloadFailedError({ url, status: response.status }) const buffer = await response.arrayBuffer() const archivePath = path.join(Global.Path.bin, filename) @@ -52,6 +78,8 @@ export namespace Ripgrep { }, ) await proc.exited + if (proc.exitCode !== 0) + throw new ExtractionFailedError({ filepath, stderr: proc.stderr }) } if (config.extension === "zip") { const proc = Bun.spawn( @@ -63,10 +91,13 @@ export namespace Ripgrep { }, ) await proc.exited + if (proc.exitCode !== 0) + throw new ExtractionFailedError({ + filepath: archivePath, + stderr: proc.stderr, + }) } - await fs.unlink(archivePath) - if (process.platform !== "win32") await fs.chmod(filepath, 0o755) } |
