Skip to content

Commit

Permalink
feat: improve error message
Browse files Browse the repository at this point in the history
  • Loading branch information
rei1024 committed Oct 23, 2024
1 parent fd088ec commit 2d6c1a5
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/lib/runOscillator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ export type RunOscillatorResult = {
world: WorldWithHistory;
};

export class MaxGenerationError extends Error {
constructor(maxGen: number) {
super("Maximum generation is " + maxGen);
}
}

export function runOscillator(
config: RunOscillatorConfig
): RunOscillatorResult {
Expand All @@ -28,7 +34,7 @@ export function runOscillator(
forceStop: () => world.getGen() >= maxGeneration,
});
if (result === "forced-stop") {
throw new Error("Max Generations.");
throw new MaxGenerationError(config.maxGeneration);
}
return {
world,
Expand Down
11 changes: 9 additions & 2 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { analyzeOscillator, type AnalyzeResult } from "./lib/analyzeOscillator";
import { parseRLE } from "@ca-ts/rle";
import { parseRule } from "@ca-ts/rule";
import { MaxGenerationError } from "./lib/runOscillator";

export type WorkerRequestMessage = {
kind: "request-analyze";
Expand Down Expand Up @@ -56,19 +57,25 @@ function handleRequest(data: WorkerRequestMessage): WorkerResponseMessage {
message: "Empty pattern",
};
}

const maxGeneration = rule.type === "int" ? 2_000 : 50_000;
try {
const result = analyzeOscillator({
cells: cells,
rule:
rule.type === "int"
? { intTransition: rule.transition }
: { transition: rule.transition },
maxGeneration: rule.type === "int" ? 2_000 : 50_000,
maxGeneration: maxGeneration,
});
return { kind: "response-analyzed", data: result };
} catch (error) {
console.error(error);
if (error instanceof MaxGenerationError) {
return {
kind: "response-error",
message: `maximum period is ${maxGeneration.toLocaleString()}`,
};
}
return {
kind: "response-error",
message: "Analyzation Error",
Expand Down

0 comments on commit 2d6c1a5

Please sign in to comment.