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
9 changes: 8 additions & 1 deletion rest/nodejs/src/api/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

import { ResourceNotFoundError, ucpErrorResponse } from "../utils/ucp_error";
import { type IdParamContext } from "../utils/validation";
import { CheckoutService } from "./checkout";

Expand All @@ -33,7 +34,13 @@ export class TestingService {
return c.json({ status: "shipped" }, 200);
} catch (e: any) {
if (e.message === "Order not found") {
return c.json({ detail: "Order not found" }, 404);
// The Python reference answers this with the UCP error envelope
// (services/checkout_service.py ship_order raises
// ResourceNotFoundError -> server.py ucp_exception_handler).
return ucpErrorResponse(
c,
new ResourceNotFoundError("Order not found")
);
}
return c.json({ detail: e.message }, 500);
}
Expand Down
27 changes: 27 additions & 0 deletions rest/nodejs/test/error_envelope.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { zValidator } from "@hono/zod-validator";
import { Hono } from "hono";

import { CheckoutService } from "../src/api/checkout";
import { TestingService } from "../src/api/testing";
import { getProductsDb, getTransactionsDb, initDbs } from "../src/data/db";
import {
CheckoutCompleteRequestSchema,
Expand Down Expand Up @@ -67,6 +68,12 @@ function buildApp() {
zValidator("param", IdParamSchema, prettyValidation),
svc.cancelCheckout
);
const testingSvc = new TestingService(svc);
app.post(
"/testing/simulate-shipping/:id",
zValidator("param", IdParamSchema, prettyValidation),
testingSvc.shipOrder
);
return app;
}

Expand Down Expand Up @@ -167,6 +174,26 @@ test("unknown checkout id answers 404 with a RESOURCE_NOT_FOUND envelope", async
assertUcpError((await res.json()) as UcpErrorBody, "RESOURCE_NOT_FOUND");
});

// The Python reference envelopes this same failure through
// ResourceNotFoundError (services/checkout_service.py ship_order ->
// server.py ucp_exception_handler); the Node twin answered a flat
// { detail } until this test's fix.
test("simulate-shipping an unknown order answers 404 with a RESOURCE_NOT_FOUND envelope", async () => {
const app = buildApp();
const res = await app.request(
"/testing/simulate-shipping/ord_missing_envelope",
{
method: "POST",
headers: {
"Simulation-Secret":
process.env.SIMULATION_SECRET || "super-secret-sim-key",
},
}
);
assert.equal(res.status, 404);
assertUcpError((await res.json()) as UcpErrorBody, "RESOURCE_NOT_FOUND");
});

test("updating a canceled checkout answers 409 with a CHECKOUT_NOT_MODIFIABLE envelope", async () => {
const app = buildApp();
const created = (await (await create(app)).json()) as { id: string };
Expand Down
Loading