Skip to content

Commit

Permalink
fix: catch 404 errors that could be thrown from github
Browse files Browse the repository at this point in the history
Signed-off-by: tylerslaton <[email protected]>
  • Loading branch information
tylerslaton committed Mar 14, 2024
1 parent 8b9df91 commit 1812d61
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions src/server/api/[...slug].post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,31 +90,35 @@ export default defineEventHandler(async (event) => {
// getExamples fetches the examples from the repo located at github.com/owner/repo and returns them as an array of ToolExample objects
const getExamples = async (owner: string, repo: string): Promise<ToolExample[]> => {
const octokit = new Octokit({ auth: useRuntimeConfig().githubToken});

// Get the contents of the examples directory
const response = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: 'examples',
ref: 'main' // Replace 'main' with the desired branch name
});

// Filter the response to include only .gpt files
const gptExamples = (response.data as any[]).filter((file: any) => file.type === 'file' && file.name.endsWith('.gpt'));

const gptFiles: ToolExample[] = await Promise.all(gptExamples.map(async (example: any) => {
try {
// Get the contents of the examples directory
const response = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: example.path,
path: 'examples',
ref: 'main' // Replace 'main' with the desired branch name
});

const content = Buffer.from((response.data as any).content, 'base64').toString();
const githubLink = `https://github.com/${owner}/${repo}/blob/main/${example.path}`;
// Filter the response to include only .gpt files
const gptExamples = (response.data as any[]).filter((file: any) => file.type === 'file' && file.name.endsWith('.gpt'));

const gptFiles: ToolExample[] = await Promise.all(gptExamples.map(async (example: any) => {
const response = await octokit.request('GET /repos/{owner}/{repo}/contents/{path}', {
owner,
repo,
path: example.path,
ref: 'main' // Replace 'main' with the desired branch name
});

const content = Buffer.from((response.data as any).content, 'base64').toString();
const githubLink = `https://github.com/${owner}/${repo}/blob/main/${example.path}`;

return { name: example.name, url: githubLink, content };
}));
return { name: example.name, url: githubLink, content };
}));

return gptFiles;
return gptFiles;
} catch (e) {
return [];
}
};

0 comments on commit 1812d61

Please sign in to comment.