Add structured environment label errors
Co-authored-by: Julius Marminge <[email protected]>
This commit is contained in:
1 parent
aad713aac1
commit
6240442f65
2 files changed
+36
-4
No files matched your search
@@ -4,6 +4,7 @@ import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
|
||||
import {
|
||||
ServerEnvironmentLabelCommandError,
|
||||
ServerEnvironmentLabelCommandRunner,
|
||||
resolveServerEnvironmentLabel,
|
||||
} from "./ServerEnvironmentLabel.ts";
|
||||
@@ -20,7 +21,10 @@ function commandRunnerLayer(input: {
|
||||
readonly run: (
|
||||
command: string,
|
||||
args: readonly string[],
|
||||
) => Effect.Effect<{ readonly stdout: string; readonly exitCode: number }, unknown>;
|
||||
) => Effect.Effect<
|
||||
{ readonly stdout: string; readonly exitCode: number },
|
||||
ServerEnvironmentLabelCommandError
|
||||
>;
|
||||
}) {
|
||||
return Layer.mock(ServerEnvironmentLabelCommandRunner)({
|
||||
run: (command, args) =>
|
||||
@@ -143,7 +147,14 @@ describe("resolveServerEnvironmentLabel", () => {
|
||||
Effect.provide(
|
||||
testLayer(
|
||||
commandRunnerLayer({
|
||||
run: () => Effect.fail(new Error("spawn scutil ENOENT")),
|
||||
run: (command, args) =>
|
||||
Effect.fail(
|
||||
new ServerEnvironmentLabelCommandError({
|
||||
command,
|
||||
args: [...args],
|
||||
message: "spawn scutil ENOENT",
|
||||
}),
|
||||
),
|
||||
}),
|
||||
),
|
||||
),
|
||||
|
||||
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
|
||||
|
||||
import { collectUint8StreamText } from "../../stream/collectUint8StreamText.ts";
|
||||
@@ -24,9 +25,19 @@ interface ServerEnvironmentLabelCommandRunnerShape {
|
||||
readonly run: (
|
||||
command: string,
|
||||
args: readonly string[],
|
||||
) => Effect.Effect<ServerEnvironmentLabelCommandResult, unknown>;
|
||||
) => Effect.Effect<ServerEnvironmentLabelCommandResult, ServerEnvironmentLabelCommandError>;
|
||||
}
|
||||
|
||||
export class ServerEnvironmentLabelCommandError extends Schema.TaggedErrorClass<ServerEnvironmentLabelCommandError>()(
|
||||
"ServerEnvironmentLabelCommandError",
|
||||
{
|
||||
command: Schema.String,
|
||||
args: Schema.Array(Schema.String),
|
||||
message: Schema.String,
|
||||
cause: Schema.optional(Schema.Defect),
|
||||
},
|
||||
) {}
|
||||
|
||||
export class ServerEnvironmentLabelCommandRunner extends Context.Service<
|
||||
ServerEnvironmentLabelCommandRunner,
|
||||
ServerEnvironmentLabelCommandRunnerShape
|
||||
@@ -59,7 +70,17 @@ export const ServerEnvironmentLabelCommandRunnerLive = Layer.effect(
|
||||
stdout: stdout.text,
|
||||
exitCode: Number(exitCode),
|
||||
};
|
||||
}),
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new ServerEnvironmentLabelCommandError({
|
||||
command,
|
||||
args: [...args],
|
||||
message: `Failed to run friendly host label command: ${command}.`,
|
||||
cause,
|
||||
}),
|
||||
),
|
||||
),
|
||||
),
|
||||
});
|
||||
}),
|
||||
|
||||
Reference in new issue
Block a user