Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/pty-proxy/src/controller/agent-socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class AgentSocket {
"name" | "version" | "kind" | "identifier" | "workspaceId"
>,
) {
const [res] = await upsertResources(db, [
const { updated } = await upsertResources(db, [
{
...resource,
name: this.name,
Expand All @@ -136,6 +136,7 @@ export class AgentSocket {
updatedAt: new Date(),
},
]);
const res = updated.at(0);
if (res == null) throw new Error("Failed to create resource");
this.resource = res;
agents.set(res.id, { lastSync: new Date(), agent: this });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,10 @@ export const PATCH = request()
{ status: 404 },
);

const t = await upsertResources(db, [_.merge(resource, body)]);

return NextResponse.json(t[0]);
const { updated } = await upsertResources(db, [_.merge(resource, body)]);
const res = updated.at(0);
if (res == null) throw new Error("Failed to update resource");
return NextResponse.json(res);
Comment on lines +96 to +99
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Improve error handling for resource update failures

The current implementation throws an error when the update fails, which will result in a 500 Internal Server Error. Consider returning a more appropriate HTTP response instead.

Consider this improvement:

-    const { updated } = await upsertResources(db, [_.merge(resource, body)]);
-    const res = updated.at(0);
-    if (res == null) throw new Error("Failed to update resource");
-    return NextResponse.json(res);
+    const { updated } = await upsertResources(db, [_.merge(resource, body)]);
+    const res = updated.at(0);
+    if (res == null) {
+      return NextResponse.json(
+        { error: "Failed to update resource" },
+        { status: 422 }
+      );
+    }
+    return NextResponse.json(res);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { updated } = await upsertResources(db, [_.merge(resource, body)]);
const res = updated.at(0);
if (res == null) throw new Error("Failed to update resource");
return NextResponse.json(res);
const { updated } = await upsertResources(db, [_.merge(resource, body)]);
const res = updated.at(0);
if (res == null) {
return NextResponse.json(
{ error: "Failed to update resource" },
{ status: 422 }
);
}
return NextResponse.json(res);

});

export const DELETE = request()
Expand Down
2 changes: 1 addition & 1 deletion apps/webservice/src/app/api/v1/resources/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,6 @@ export const POST = request()
})),
);

return NextResponse.json({ count: resources.length });
return NextResponse.json({ count: resources.all.length });
},
);
4 changes: 2 additions & 2 deletions packages/api/src/router/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import {
updateEnvironment,
} from "@ctrlplane/db/schema";
import {
dispatchJobsForNewResources,
dispatchJobsForAddedResources,
getEventsForEnvironmentDeleted,
handleEvent,
} from "@ctrlplane/job-dispatch";
Expand Down Expand Up @@ -324,7 +324,7 @@ export const environmentRouter = createTRPCRouter({
}

if (newResources.length > 0) {
await dispatchJobsForNewResources(
await dispatchJobsForAddedResources(
ctx.db,
newResources.map((r) => r.id),
input.id,
Expand Down
2 changes: 0 additions & 2 deletions packages/job-dispatch/src/events/triggers/resource-deleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import { ResourceFilterType } from "@ctrlplane/validators/resources";

/**
* Get events for a resource that has been deleted.
* NOTE: Because we may need to do inner joins on resource metadata for the filter,
* this actually needs to be called before the resource is actually deleted.
* @param resource
*/
export const getEventsForResourceDeleted = async (
Expand Down
3 changes: 1 addition & 2 deletions packages/job-dispatch/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ export * from "./policy-checker.js";
export * from "./policy-create.js";
export * from "./release-sequencing.js";
export * from "./gradual-rollout.js";
export * from "./new-resource.js";
export * from "./resource.js";
export * from "./lock-checker.js";
export * from "./queue.js";

Expand All @@ -24,3 +22,4 @@ export * from "./policies/release-window.js";
export * from "./environment-creation.js";
export * from "./pending-job-checker.js";
export * from "./events/index.js";
export * from "./resource/index.js";
80 changes: 0 additions & 80 deletions packages/job-dispatch/src/new-resource.ts

This file was deleted.

Loading
Loading