Skip to content

Commit 265c62a

Browse files
committed
feat: use yieldable errors
1 parent 36e8930 commit 265c62a

File tree

7 files changed

+39
-44
lines changed

7 files changed

+39
-44
lines changed

src/create-project.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import {
33
ModuleKind,
44
ModuleResolutionKind,
@@ -12,10 +12,9 @@ export type CreateProjectOptions = {
1212
};
1313

1414
/** @internal */
15-
export class ProjectError {
16-
readonly _tag = "ProjectError";
17-
constructor(readonly cause?: unknown) {}
18-
}
15+
export class ProjectError extends Data.TaggedError("ProjectError")<{
16+
readonly cause?: unknown;
17+
}> {}
1918

2019
export const createProject = ({ indexFilePath, cwd }: CreateProjectOptions) =>
2120
Effect.try({
@@ -40,5 +39,5 @@ export const createProject = ({ indexFilePath, cwd }: CreateProjectOptions) =>
4039
project.resolveSourceFileDependencies();
4140
return { project, indexFile };
4241
},
43-
catch: (e) => new ProjectError(e),
42+
catch: (e) => new ProjectError({ cause: e }),
4443
});

src/install-package.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import { execa } from "execa";
33

44
/** @internal */
@@ -9,10 +9,9 @@ export type InstallPackageOptions = {
99
};
1010

1111
/** @internal */
12-
export class InstallPackageError {
13-
readonly _tag = "InstallPackageError";
14-
constructor(readonly cause?: unknown) {}
15-
}
12+
export class InstallPackageError extends Data.TaggedError(
13+
"InstallPackageError",
14+
)<{ cause?: unknown }> {}
1615

1716
/** @internal */
1817
export const installPackage = ({
@@ -28,7 +27,7 @@ export const installPackage = ({
2827
const bunAdd = ({ pkg, cwd, bunPath }: Required<InstallPackageOptions>) =>
2928
Effect.tryPromise({
3029
try: () => execa(bunPath, ["add", pkg, "--verbose"], { cwd }),
31-
catch: (e) => new InstallPackageError(e),
30+
catch: (e) => new InstallPackageError({ cause: e }),
3231
});
3332

3433
const installedPackages = (stdout: string) => {

src/package-declarations.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import type { Project, SourceFile } from "ts-morph";
33
import { extractDeclarations } from "./extract-declarations";
44

@@ -10,10 +10,9 @@ export type PackageDeclarationsOptions = {
1010
};
1111

1212
/** @internal */
13-
export class PackageDeclarationsError {
14-
readonly _tag = "PackageDeclarationsError";
15-
constructor(readonly cause?: unknown) {}
16-
}
13+
export class PackageDeclarationsError extends Data.TaggedError(
14+
"PackageDeclarationsError",
15+
)<{ cause?: unknown }> {}
1716

1817
export const packageDeclarations = ({
1918
pkgName,
@@ -30,5 +29,5 @@ export const packageDeclarations = ({
3029
project,
3130
pkgName,
3231
}),
33-
catch: (e) => new PackageDeclarationsError(e),
32+
catch: (e) => new PackageDeclarationsError({ cause: e }),
3433
});

src/package-json.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import { readPackage } from "read-pkg";
33

44
/** @internal */
5-
export class PackageJsonError {
6-
readonly _tag = "PackageJsonError";
7-
constructor(readonly cause?: unknown) {}
8-
}
5+
export class PackageJsonError extends Data.TaggedError("PackageJsonError")<{
6+
cause?: unknown;
7+
}> {}
98

109
/** @internal */
1110
export const packageJson = (pkgDir: string) =>
1211
Effect.tryPromise({
1312
try: () => readPackage({ cwd: pkgDir }),
14-
catch: (e) => new PackageJsonError(e),
13+
catch: (e) => new PackageJsonError({ cause: e }),
1514
});

src/package-name.ts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import validate from "validate-npm-package-name";
33

44
/** @internal */
5-
export class PackageNameError {
6-
readonly _tag = "PackageNameError";
7-
}
5+
export class PackageNameError extends Data.TaggedError("PackageNameError")<{
6+
warnings?: string[];
7+
errors?: string[];
8+
}> {}
89

910
/** @internal */
1011
export const packageName = (pkg: string) =>
11-
Effect.suspend(() => {
12+
Effect.gen(function* (_) {
1213
const versionMarker = pkg.lastIndexOf("@");
13-
const name = pkg.slice(0, versionMarker > 0 ? versionMarker : undefined);
14-
if (!validate(name).validForNewPackages) {
15-
return Effect.fail(new PackageNameError());
14+
const pkgName = pkg.slice(0, versionMarker > 0 ? versionMarker : undefined);
15+
const { validForNewPackages, warnings, errors } = validate(pkgName);
16+
if (!validForNewPackages) {
17+
return yield* _(new PackageNameError({ warnings, errors }));
1618
}
17-
return Effect.succeed(name);
19+
return pkgName;
1820
});

src/package-types.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import type { NormalizedPackageJson } from "read-pkg";
33
import { exports } from "resolve.exports";
44

55
/** @internal */
6-
export class PackageTypesError {
7-
readonly _tag = "PackageTypesError";
8-
}
6+
export class PackageTypesError extends Data.TaggedError("PackageTypesError") {}
97

108
/**
119
`packageTypes` resolves the types entrypoint file (e.g., `index.d.ts`).
@@ -32,7 +30,7 @@ export const packageTypes = (
3230
if (isRootSubpath && pkgJson.typings && isTypesFile(pkgJson.typings)) {
3331
return pkgJson.typings;
3432
}
35-
return yield* _(Effect.fail(new PackageTypesError()));
33+
return yield* _(new PackageTypesError());
3634
});
3735

3836
const resolveExports = (

src/work-dir.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Effect } from "effect";
1+
import { Data, Effect } from "effect";
22
import { rm } from "node:fs/promises";
33
import { temporaryDirectory } from "tempy";
44

@@ -9,10 +9,9 @@ export type WorkDir = {
99
};
1010

1111
/** @internal */
12-
export class WorkDirError {
13-
readonly _tag = "WorkDirError";
14-
constructor(readonly cause?: unknown) {}
15-
}
12+
export class WorkDirError extends Data.TaggedError("WorkDirError")<{
13+
cause?: unknown;
14+
}> {}
1615

1716
const acquire = Effect.try({
1817
try: () => {
@@ -26,7 +25,7 @@ const acquire = Effect.try({
2625
},
2726
};
2827
},
29-
catch: (e) => new WorkDirError(e),
28+
catch: (e) => new WorkDirError({ cause: e }),
3029
});
3130

3231
const release = (workDir: WorkDir) => Effect.promise(() => workDir.close());

0 commit comments

Comments
 (0)