Skip to content

Commit

Permalink
(fix): try catch any snippet load failures (#689)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsinghvi authored Apr 22, 2024
1 parent 89b8f37 commit 04336fa
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 20 deletions.
9 changes: 9 additions & 0 deletions fern/apis/fdr/generators.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,15 @@ groups:
include_union_utils: true
client:
class_name: FdrClient
- name: fernapi/fern-typescript-node-sdk
version: 0.12.8-rc0
output:
location: npm
url: npm.buildwithfern.com
package-name: "@fern-fern/snippet-template-sdk"
config:
namespaceExport: FdrSnippetTemplate
includeUtilsOnUnionMembers: true

fiddle:
audiences:
Expand Down
7 changes: 7 additions & 0 deletions servers/fdr/src/__test__/local/services/snippets.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ it("get snippets", async () => {
// get snippets
const snippets = getAPIResponse(
await fdr.get({
orgId: "acme",
apiId: "foo",
endpoint: {
path: "/snippets/load",
Expand Down Expand Up @@ -150,6 +151,7 @@ it("get Go snippets", async () => {
const snippets = getAPIResponse(
await fdr.get({
apiId: "echo",
orgId: "acme",
endpoint: {
path: "/snippets/load",
method: FdrAPI.EndpointMethod.Post,
Expand Down Expand Up @@ -260,6 +262,7 @@ it("get snippets with unregistered API", async () => {
// get snippets
const snippets = getAPIResponse(
await fdr.get({
orgId: "acme",
apiId: "fresh",
endpoint: {
path: "/users/v1",
Expand Down Expand Up @@ -313,6 +316,7 @@ it("load snippets", async () => {
// load snippets (first page)
const firstResponse = getAPIResponse(
await fdr.load({
orgId: "acme",
apiId: "petstore",
}),
);
Expand All @@ -335,6 +339,7 @@ it("load snippets", async () => {
// load snippets (second page)
const secondResponse = getAPIResponse(
await fdr.load({
orgId: "acme",
apiId: "petstore",
sdks: [
{
Expand Down Expand Up @@ -429,6 +434,7 @@ it("snippets apiId not found", async () => {

// get not found apiId
const response = await fdr.get({
orgId: "acme",
apiId: "dne",
endpoint: {
path: "/users/v1",
Expand Down Expand Up @@ -472,6 +478,7 @@ it("get snippets (unauthenticated)", async () => {
// get snippets
const unauthedFdr = getClient({ authed: false, url: inject("url") });
const response = await unauthedFdr.get({
orgId: "acme",
apiId: "user",
endpoint: {
path: "/users/v1",
Expand Down
44 changes: 24 additions & 20 deletions servers/fdr/src/controllers/snippets/getSnippetsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,31 +40,35 @@ export function getSnippetsService(app: FdrApplication): SnippetsService {
const snippetsForEndpointMethod = snippetsForEndpointPath[req.body.endpoint.method];
return res.send(snippetsForEndpointMethod ?? []);
} else {
const snippets: Snippet[] = [];
try {
const snippets: Snippet[] = [];

for (const sdk of req.body.sdks ?? []) {
const endpointSnippetTemplate: EndpointSnippetTemplate | null = await app.dao
.snippetTemplates()
.loadSnippetTemplate({
loadSnippetTemplateRequest: {
orgId: apiInfo.orgId,
apiId: apiInfo.apiId,
endpointId: req.body.endpoint,
sdk,
},
for (const sdk of req.body.sdks ?? []) {
const endpointSnippetTemplate: EndpointSnippetTemplate | null = await app.dao
.snippetTemplates()
.loadSnippetTemplate({
loadSnippetTemplateRequest: {
orgId: apiInfo.orgId,
apiId: apiInfo.apiId,
endpointId: req.body.endpoint,
sdk,
},
});
if (endpointSnippetTemplate == null) {
throw new SnippetTemplateNotFoundError("Snippet not found");
}
const templateResolver = new SnippetTemplateResolver({
payload,
endpointSnippetTemplate,
});
if (endpointSnippetTemplate == null) {
throw new SnippetTemplateNotFoundError("Snippet not found");

snippets.push(templateResolver.resolve());
}
const templateResolver = new SnippetTemplateResolver({
payload,
endpointSnippetTemplate,
});

snippets.push(templateResolver.resolve());
return res.send(snippets);
} catch (e) {
return res.send([]);
}

return res.send(snippets);
}
},
load: async (req, res) => {
Expand Down

0 comments on commit 04336fa

Please sign in to comment.