Skip to content

Commit bf18fd0

Browse files
authored
Merge pull request #165 from microsoft/users/danhellem/wiki-errorhandling-1
Enhance error handling and add tests for wiki tools
2 parents 5aa11ed + 726984f commit bf18fd0

2 files changed

Lines changed: 389 additions & 71 deletions

File tree

src/tools/wiki.ts

Lines changed: 101 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ function configureWikiTools(
2727
project: z.string().optional().describe("The project name or ID where the wiki is located. If not provided, the default project will be used."),
2828
},
2929
async ({ wikiIdentifier, project }) => {
30-
const connection = await connectionProvider();
31-
const wikiApi = await connection.getWikiApi();
32-
const wiki = await wikiApi.getWiki(wikiIdentifier, project);
33-
34-
return {
35-
content: [{ type: "text", text: JSON.stringify(wiki, null, 2) }],
36-
};
30+
try {
31+
const connection = await connectionProvider();
32+
const wikiApi = await connection.getWikiApi();
33+
const wiki = await wikiApi.getWiki(wikiIdentifier, project);
34+
35+
if (!wiki) {
36+
return { content: [{ type: "text", text: "No wiki found" }], isError: true };
37+
}
38+
39+
return {
40+
content: [{ type: "text", text: JSON.stringify(wiki, null, 2) }],
41+
};
42+
} catch (error) {
43+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
44+
45+
return {
46+
content: [{ type: "text", text: `Error fetching wiki: ${errorMessage}` }],
47+
isError: true
48+
};
49+
}
3750
}
3851
);
3952

@@ -44,13 +57,26 @@ function configureWikiTools(
4457
project: z.string().optional().describe("The project name or ID to filter wikis. If not provided, all wikis in the organization will be returned."),
4558
},
4659
async ({ project }) => {
47-
const connection = await connectionProvider();
48-
const wikiApi = await connection.getWikiApi();
49-
const wikis = await wikiApi.getAllWikis(project);
50-
51-
return {
52-
content: [{ type: "text", text: JSON.stringify(wikis, null, 2) }],
53-
};
60+
try {
61+
const connection = await connectionProvider();
62+
const wikiApi = await connection.getWikiApi();
63+
const wikis = await wikiApi.getAllWikis(project);
64+
65+
if (!wikis) {
66+
return { content: [{ type: "text", text: "No wikis found" }], isError: true };
67+
}
68+
69+
return {
70+
content: [{ type: "text", text: JSON.stringify(wikis, null, 2) }],
71+
};
72+
} catch (error) {
73+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
74+
75+
return {
76+
content: [{ type: "text", text: `Error fetching wikis: ${errorMessage}` }],
77+
isError: true
78+
};
79+
}
5480
}
5581
);
5682

@@ -71,24 +97,37 @@ function configureWikiTools(
7197
continuationToken,
7298
pageViewsForDays,
7399
}) => {
74-
const connection = await connectionProvider();
75-
const wikiApi = await connection.getWikiApi();
76-
77-
const pagesBatchRequest: WikiPagesBatchRequest = {
78-
top,
79-
continuationToken,
80-
pageViewsForDays,
81-
};
82-
83-
const pages = await wikiApi.getPagesBatch(
84-
pagesBatchRequest,
85-
project,
86-
wikiIdentifier
87-
);
88-
89-
return {
90-
content: [{ type: "text", text: JSON.stringify(pages, null, 2) }],
91-
};
100+
try {
101+
const connection = await connectionProvider();
102+
const wikiApi = await connection.getWikiApi();
103+
104+
const pagesBatchRequest: WikiPagesBatchRequest = {
105+
top,
106+
continuationToken,
107+
pageViewsForDays,
108+
};
109+
110+
const pages = await wikiApi.getPagesBatch(
111+
pagesBatchRequest,
112+
project,
113+
wikiIdentifier
114+
);
115+
116+
if (!pages) {
117+
return { content: [{ type: "text", text: "No wiki pages found" }], isError: true };
118+
}
119+
120+
return {
121+
content: [{ type: "text", text: JSON.stringify(pages, null, 2) }],
122+
};
123+
} catch (error) {
124+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
125+
126+
return {
127+
content: [{ type: "text", text: `Error fetching wiki pages: ${errorMessage}` }],
128+
isError: true
129+
};
130+
}
92131
}
93132
);
94133

@@ -101,23 +140,36 @@ function configureWikiTools(
101140
path: z.string().describe("The path of the wiki page to retrieve content for."),
102141
},
103142
async ({ wikiIdentifier, project, path }) => {
104-
const connection = await connectionProvider();
105-
const wikiApi = await connection.getWikiApi();
106-
107-
const stream = await wikiApi.getPageText(
108-
project,
109-
wikiIdentifier,
110-
path,
111-
undefined,
112-
undefined,
113-
true
114-
);
115-
116-
const content = await streamToString(stream);
117-
118-
return {
119-
content: [{ type: "text", text: JSON.stringify(content, null, 2) }],
120-
};
143+
try {
144+
const connection = await connectionProvider();
145+
const wikiApi = await connection.getWikiApi();
146+
147+
const stream = await wikiApi.getPageText(
148+
project,
149+
wikiIdentifier,
150+
path,
151+
undefined,
152+
undefined,
153+
true
154+
);
155+
156+
if (!stream) {
157+
return { content: [{ type: "text", text: "No wiki page content found" }], isError: true };
158+
}
159+
160+
const content = await streamToString(stream);
161+
162+
return {
163+
content: [{ type: "text", text: JSON.stringify(content, null, 2) }],
164+
};
165+
} catch (error) {
166+
const errorMessage = error instanceof Error ? error.message : 'Unknown error occurred';
167+
168+
return {
169+
content: [{ type: "text", text: `Error fetching wiki page content: ${errorMessage}` }],
170+
isError: true
171+
};
172+
}
121173
}
122174
);
123175
}

0 commit comments

Comments
 (0)