Skip to content

Commit 6240442

Browse files
Add structured environment label errors
Co-authored-by: Julius Marminge <juliusmarminge@users.noreply.github.com>
1 parent aad713a commit 6240442

2 files changed

Lines changed: 36 additions & 4 deletions

File tree

apps/server/src/environment/Layers/ServerEnvironmentLabel.test.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as FileSystem from "effect/FileSystem";
44
import * as Layer from "effect/Layer";
55

66
import {
7+
ServerEnvironmentLabelCommandError,
78
ServerEnvironmentLabelCommandRunner,
89
resolveServerEnvironmentLabel,
910
} from "./ServerEnvironmentLabel.ts";
@@ -20,7 +21,10 @@ function commandRunnerLayer(input: {
2021
readonly run: (
2122
command: string,
2223
args: readonly string[],
23-
) => Effect.Effect<{ readonly stdout: string; readonly exitCode: number }, unknown>;
24+
) => Effect.Effect<
25+
{ readonly stdout: string; readonly exitCode: number },
26+
ServerEnvironmentLabelCommandError
27+
>;
2428
}) {
2529
return Layer.mock(ServerEnvironmentLabelCommandRunner)({
2630
run: (command, args) =>
@@ -143,7 +147,14 @@ describe("resolveServerEnvironmentLabel", () => {
143147
Effect.provide(
144148
testLayer(
145149
commandRunnerLayer({
146-
run: () => Effect.fail(new Error("spawn scutil ENOENT")),
150+
run: (command, args) =>
151+
Effect.fail(
152+
new ServerEnvironmentLabelCommandError({
153+
command,
154+
args: [...args],
155+
message: "spawn scutil ENOENT",
156+
}),
157+
),
147158
}),
148159
),
149160
),

apps/server/src/environment/Layers/ServerEnvironmentLabel.ts

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Effect from "effect/Effect";
55
import * as FileSystem from "effect/FileSystem";
66
import * as Layer from "effect/Layer";
77
import * as Option from "effect/Option";
8+
import * as Schema from "effect/Schema";
89
import { ChildProcess, ChildProcessSpawner } from "effect/unstable/process";
910

1011
import { collectUint8StreamText } from "../../stream/collectUint8StreamText.ts";
@@ -24,9 +25,19 @@ interface ServerEnvironmentLabelCommandRunnerShape {
2425
readonly run: (
2526
command: string,
2627
args: readonly string[],
27-
) => Effect.Effect<ServerEnvironmentLabelCommandResult, unknown>;
28+
) => Effect.Effect<ServerEnvironmentLabelCommandResult, ServerEnvironmentLabelCommandError>;
2829
}
2930

31+
export class ServerEnvironmentLabelCommandError extends Schema.TaggedErrorClass<ServerEnvironmentLabelCommandError>()(
32+
"ServerEnvironmentLabelCommandError",
33+
{
34+
command: Schema.String,
35+
args: Schema.Array(Schema.String),
36+
message: Schema.String,
37+
cause: Schema.optional(Schema.Defect),
38+
},
39+
) {}
40+
3041
export class ServerEnvironmentLabelCommandRunner extends Context.Service<
3142
ServerEnvironmentLabelCommandRunner,
3243
ServerEnvironmentLabelCommandRunnerShape
@@ -59,7 +70,17 @@ export const ServerEnvironmentLabelCommandRunnerLive = Layer.effect(
5970
stdout: stdout.text,
6071
exitCode: Number(exitCode),
6172
};
62-
}),
73+
}).pipe(
74+
Effect.mapError(
75+
(cause) =>
76+
new ServerEnvironmentLabelCommandError({
77+
command,
78+
args: [...args],
79+
message: `Failed to run friendly host label command: ${command}.`,
80+
cause,
81+
}),
82+
),
83+
),
6384
),
6485
});
6586
}),

0 commit comments

Comments
 (0)