Skip to content

Commit 5e868b1

Browse files
committed
chore: add ActionError for expected user errors
1 parent f79c5cd commit 5e868b1

12 files changed

Lines changed: 144 additions & 39 deletions

dist/build.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/checkoutPRRef.js

Lines changed: 16 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js

Lines changed: 19 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/merge.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/prepareCombine.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/prepareSwagger.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/preview.js

Lines changed: 14 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/checkoutPRRef.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import spawn from "nano-spawn";
22
import { getInput } from "./compat";
33
import { getMergeBase, saveConfig } from "./config";
4+
import { ActionError } from "./error";
45
import { logger } from "./logger";
56

67
function assertRef(ref: string): asserts ref is "base" | "head" {
78
if (ref !== "base" && ref !== "head") {
8-
throw new Error(`Expected ref to be 'base' or 'head', but was ${ref}`);
9+
throw new ActionError(`Expected ref to be 'base' or 'head', but was ${ref}`);
910
}
1011
}
1112

@@ -32,7 +33,7 @@ async function main() {
3233
// Callers come in from checkout-pr-ref against base; save the config.
3334
const { hasOAS, savedSha } = await saveConfig({ oasPath, configPath });
3435
if (!hasOAS) {
35-
throw new Error(`Expected OpenAPI spec at ${oasPath}.`);
36+
throw new ActionError(`Expected OpenAPI spec at ${oasPath}.`);
3637
}
3738
if (savedSha !== null && savedSha !== mergeBaseSha) {
3839
logger.warn(

src/error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* An error that represents an expected failure in the action (e.g. invalid input,
3+
* missing files, auth failures). These are distinguished from unexpected internal
4+
* errors so that we don't prompt users to file a bug report.
5+
*/
6+
export class ActionError extends Error {
7+
constructor(message: string, options?: ErrorOptions) {
8+
super(message, options);
9+
this.name = "ActionError";
10+
}
11+
}

src/index.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getStainlessClient } from "./stainless";
33
import { readFileSync, writeFileSync } from "node:fs";
44
import YAML from "yaml";
55
import { getBooleanInput, getInput, getStainlessAuth } from "./compat";
6+
import { ActionError } from "./error";
67
import { logger } from "./logger";
78

89
// https://www.conventionalcommits.org/en/v1.0.0/
@@ -30,14 +31,14 @@ export async function main() {
3031
if (configPath && guessConfig) {
3132
const errorMsg = "Can't set both configPath and guessConfig";
3233
logger.error(errorMsg);
33-
throw Error(errorMsg);
34+
throw new ActionError(errorMsg);
3435
}
3536

3637
if (commitMessage && !isValidConventionalCommitMessage(commitMessage)) {
3738
const errorMsg =
3839
"Invalid commit message format. Please follow the Conventional Commits format: https://www.conventionalcommits.org/en/v1.0.0/";
3940
logger.error(errorMsg);
40-
throw Error(errorMsg);
41+
throw new ActionError(errorMsg);
4142
}
4243

4344
if (!projectName) {
@@ -48,7 +49,7 @@ export async function main() {
4849
if (projects.data.length === 0) {
4950
const errorMsg = "No projects found. Please create a project first.";
5051
logger.error(errorMsg);
51-
throw Error(errorMsg);
52+
throw new ActionError(errorMsg);
5253
}
5354
projectName = projects.data[0]!.slug;
5455
if (projects.data.length > 1) {
@@ -77,15 +78,15 @@ export async function main() {
7778
response.errors,
7879
)} See more details in the Stainless Studio.`;
7980
logger.error(errorMsg);
80-
throw Error(errorMsg);
81+
throw new ActionError(errorMsg);
8182
}
8283
logger.info("Uploaded!");
8384

8485
if (outputPath) {
8586
if (!response.decoratedSpec) {
8687
const errorMsg = "Failed to get decorated spec";
8788
logger.error(errorMsg);
88-
throw Error(errorMsg);
89+
throw new ActionError(errorMsg);
8990
}
9091
// Decorated spec is currently always YAML, so convert it to JSON if needed.
9192
if (!(outputPath.endsWith(".yml") || outputPath.endsWith(".yaml"))) {

0 commit comments

Comments
 (0)