Skip to content

Commit 3466109

Browse files
committed
fix: attach source path to skill parse errors
1 parent 80dbe3b commit 3466109

2 files changed

Lines changed: 71 additions & 36 deletions

File tree

‎src/skills/skill.ts‎

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@ import { parse as parseYaml } from "yaml";
33

44
export class SkillParseError extends Error {
55
readonly field?: string;
6+
readonly path?: string;
67

7-
constructor(message: string, field?: string) {
8+
constructor(message: string, field?: string, filePath?: string) {
89
super(message);
910
this.name = "SkillParseError";
1011
this.field = field;
12+
this.path = filePath;
1113
}
1214
}
1315

@@ -34,41 +36,57 @@ type ParseOptions = {
3436
};
3537

3638
export function parseSkillMarkdown(markdown: string, options: ParseOptions = {}): SkillFile {
37-
const { frontmatterText, body } = splitFrontmatter(markdown);
38-
const data = parseYaml(frontmatterText);
39-
40-
if (!data || typeof data !== "object" || Array.isArray(data)) {
41-
throw new SkillParseError("Frontmatter must be a YAML object");
42-
}
43-
44-
const record = data as Record<string, unknown>;
45-
const name = readStringField(record, "name", true);
46-
const description = readStringField(record, "description", true);
47-
const license = readStringField(record, "license");
48-
const compatibility = readStringField(record, "compatibility");
49-
const allowedTools = readAllowedTools(record);
50-
const metadata = normalizeMetadata(record.metadata);
51-
52-
const expectedName = options.expectedName ?? deriveExpectedName(options.path);
53-
54-
validateName(name, expectedName);
55-
validateDescription(description);
56-
validateCompatibility(compatibility);
57-
58-
const frontmatter: SkillFrontmatter = {
59-
name,
60-
description,
61-
license,
62-
compatibility,
63-
metadata,
64-
allowedTools,
65-
};
66-
67-
return {
68-
frontmatter,
69-
body,
70-
isInternal: metadata?.internal === true,
71-
};
39+
try {
40+
const { frontmatterText, body } = splitFrontmatter(markdown);
41+
const data = parseYaml(frontmatterText);
42+
43+
if (!data || typeof data !== "object" || Array.isArray(data)) {
44+
throw new SkillParseError("Frontmatter must be a YAML object");
45+
}
46+
47+
const record = data as Record<string, unknown>;
48+
const name = readStringField(record, "name", true);
49+
const description = readStringField(record, "description", true);
50+
const license = readStringField(record, "license");
51+
const compatibility = readStringField(record, "compatibility");
52+
const allowedTools = readAllowedTools(record);
53+
const metadata = normalizeMetadata(record.metadata);
54+
55+
const expectedName = options.expectedName ?? deriveExpectedName(options.path);
56+
57+
validateName(name, expectedName);
58+
validateDescription(description);
59+
validateCompatibility(compatibility);
60+
61+
const frontmatter: SkillFrontmatter = {
62+
name,
63+
description,
64+
license,
65+
compatibility,
66+
metadata,
67+
allowedTools,
68+
};
69+
70+
return {
71+
frontmatter,
72+
body,
73+
isInternal: metadata?.internal === true,
74+
};
75+
} catch (error) {
76+
if (error instanceof SkillParseError) {
77+
throw withPath(error, options.path);
78+
}
79+
80+
throw error;
81+
}
82+
}
83+
84+
function withPath(error: SkillParseError, filePath?: string): SkillParseError {
85+
if (!filePath || error.path === filePath) {
86+
return error;
87+
}
88+
89+
return new SkillParseError(error.message, error.field, filePath);
7290
}
7391

7492
function splitFrontmatter(markdown: string): { frontmatterText: string; body: string } {

‎tests/skills/skill.test.ts‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,4 +102,21 @@ describe("parseSkillMarkdown", () => {
102102
"compatibility must be 1-500 characters"
103103
);
104104
});
105+
106+
test("includes field and path in structured errors when provided", () => {
107+
const invalidDescription = VALID_SKILL.replace(
108+
"description: Sample skill for testing.",
109+
"description: "
110+
);
111+
112+
try {
113+
parseSkillMarkdown(invalidDescription, { path: "/tmp/sample-skill/SKILL.md" });
114+
throw new Error("Expected parseSkillMarkdown to throw");
115+
} catch (error) {
116+
expect(error).toBeInstanceOf(SkillParseError);
117+
const parseError = error as SkillParseError;
118+
expect(parseError.field).toBe("description");
119+
expect(parseError.path).toBe("/tmp/sample-skill/SKILL.md");
120+
}
121+
});
105122
});

0 commit comments

Comments
 (0)