@rebasepro/server-core 0.0.1-canary.000dc36
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +6 -0
- package/README.md +40 -0
- package/build-errors.txt +52 -0
- package/coverage/clover.xml +3739 -0
- package/coverage/coverage-final.json +31 -0
- package/coverage/lcov-report/base.css +224 -0
- package/coverage/lcov-report/block-navigation.js +87 -0
- package/coverage/lcov-report/favicon.png +0 -0
- package/coverage/lcov-report/index.html +266 -0
- package/coverage/lcov-report/prettify.css +1 -0
- package/coverage/lcov-report/prettify.js +2 -0
- package/coverage/lcov-report/sort-arrow-sprite.png +0 -0
- package/coverage/lcov-report/sorter.js +210 -0
- package/coverage/lcov-report/src/api/ast-schema-editor.ts.html +952 -0
- package/coverage/lcov-report/src/api/errors.ts.html +472 -0
- package/coverage/lcov-report/src/api/graphql/graphql-schema-generator.ts.html +1069 -0
- package/coverage/lcov-report/src/api/graphql/index.html +116 -0
- package/coverage/lcov-report/src/api/index.html +176 -0
- package/coverage/lcov-report/src/api/openapi-generator.ts.html +565 -0
- package/coverage/lcov-report/src/api/rest/api-generator.ts.html +994 -0
- package/coverage/lcov-report/src/api/rest/index.html +131 -0
- package/coverage/lcov-report/src/api/rest/query-parser.ts.html +550 -0
- package/coverage/lcov-report/src/api/schema-editor-routes.ts.html +202 -0
- package/coverage/lcov-report/src/api/server.ts.html +823 -0
- package/coverage/lcov-report/src/auth/admin-routes.ts.html +973 -0
- package/coverage/lcov-report/src/auth/index.html +176 -0
- package/coverage/lcov-report/src/auth/jwt.ts.html +574 -0
- package/coverage/lcov-report/src/auth/middleware.ts.html +745 -0
- package/coverage/lcov-report/src/auth/password.ts.html +310 -0
- package/coverage/lcov-report/src/auth/services.ts.html +2074 -0
- package/coverage/lcov-report/src/collections/index.html +116 -0
- package/coverage/lcov-report/src/collections/loader.ts.html +232 -0
- package/coverage/lcov-report/src/db/auth-schema.ts.html +523 -0
- package/coverage/lcov-report/src/db/data-transformer.ts.html +1753 -0
- package/coverage/lcov-report/src/db/entityService.ts.html +700 -0
- package/coverage/lcov-report/src/db/index.html +146 -0
- package/coverage/lcov-report/src/db/services/EntityFetchService.ts.html +4048 -0
- package/coverage/lcov-report/src/db/services/EntityPersistService.ts.html +883 -0
- package/coverage/lcov-report/src/db/services/RelationService.ts.html +3121 -0
- package/coverage/lcov-report/src/db/services/entity-helpers.ts.html +442 -0
- package/coverage/lcov-report/src/db/services/index.html +176 -0
- package/coverage/lcov-report/src/db/services/index.ts.html +124 -0
- package/coverage/lcov-report/src/generate-drizzle-schema-logic.ts.html +1960 -0
- package/coverage/lcov-report/src/index.html +116 -0
- package/coverage/lcov-report/src/services/driver-registry.ts.html +631 -0
- package/coverage/lcov-report/src/services/index.html +131 -0
- package/coverage/lcov-report/src/services/postgresDataDriver.ts.html +3025 -0
- package/coverage/lcov-report/src/storage/LocalStorageController.ts.html +1189 -0
- package/coverage/lcov-report/src/storage/S3StorageController.ts.html +970 -0
- package/coverage/lcov-report/src/storage/index.html +161 -0
- package/coverage/lcov-report/src/storage/storage-registry.ts.html +646 -0
- package/coverage/lcov-report/src/storage/types.ts.html +451 -0
- package/coverage/lcov-report/src/utils/drizzle-conditions.ts.html +3082 -0
- package/coverage/lcov-report/src/utils/index.html +116 -0
- package/coverage/lcov.info +7179 -0
- package/dist/common/src/collections/CollectionRegistry.d.ts +56 -0
- package/dist/common/src/collections/index.d.ts +1 -0
- package/dist/common/src/data/buildRebaseData.d.ts +14 -0
- package/dist/common/src/index.d.ts +3 -0
- package/dist/common/src/util/builders.d.ts +57 -0
- package/dist/common/src/util/callbacks.d.ts +6 -0
- package/dist/common/src/util/collections.d.ts +11 -0
- package/dist/common/src/util/common.d.ts +2 -0
- package/dist/common/src/util/conditions.d.ts +26 -0
- package/dist/common/src/util/entities.d.ts +58 -0
- package/dist/common/src/util/enums.d.ts +3 -0
- package/dist/common/src/util/index.d.ts +16 -0
- package/dist/common/src/util/navigation_from_path.d.ts +34 -0
- package/dist/common/src/util/navigation_utils.d.ts +20 -0
- package/dist/common/src/util/parent_references_from_path.d.ts +6 -0
- package/dist/common/src/util/paths.d.ts +14 -0
- package/dist/common/src/util/permissions.d.ts +5 -0
- package/dist/common/src/util/references.d.ts +2 -0
- package/dist/common/src/util/relations.d.ts +22 -0
- package/dist/common/src/util/resolutions.d.ts +72 -0
- package/dist/common/src/util/storage.d.ts +24 -0
- package/dist/index-DXVBFp5V.js +37 -0
- package/dist/index-DXVBFp5V.js.map +1 -0
- package/dist/index.es.js +49249 -0
- package/dist/index.es.js.map +1 -0
- package/dist/index.umd.js +49283 -0
- package/dist/index.umd.js.map +1 -0
- package/dist/server-core/src/api/ast-schema-editor.d.ts +21 -0
- package/dist/server-core/src/api/collections_for_test/callbacks_test_collection.d.ts +2 -0
- package/dist/server-core/src/api/errors.d.ts +35 -0
- package/dist/server-core/src/api/graphql/graphql-schema-generator.d.ts +35 -0
- package/dist/server-core/src/api/graphql/index.d.ts +1 -0
- package/dist/server-core/src/api/index.d.ts +9 -0
- package/dist/server-core/src/api/openapi-generator.d.ts +16 -0
- package/dist/server-core/src/api/rest/api-generator.d.ts +76 -0
- package/dist/server-core/src/api/rest/index.d.ts +1 -0
- package/dist/server-core/src/api/rest/query-parser.d.ts +9 -0
- package/dist/server-core/src/api/schema-editor-routes.d.ts +3 -0
- package/dist/server-core/src/api/server.d.ts +40 -0
- package/dist/server-core/src/api/types.d.ts +90 -0
- package/dist/server-core/src/auth/admin-routes.d.ts +21 -0
- package/dist/server-core/src/auth/apple-oauth.d.ts +30 -0
- package/dist/server-core/src/auth/bitbucket-oauth.d.ts +11 -0
- package/dist/server-core/src/auth/discord-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/facebook-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/github-oauth.d.ts +15 -0
- package/dist/server-core/src/auth/gitlab-oauth.d.ts +13 -0
- package/dist/server-core/src/auth/google-oauth.d.ts +14 -0
- package/dist/server-core/src/auth/index.d.ts +23 -0
- package/dist/server-core/src/auth/interfaces.d.ts +309 -0
- package/dist/server-core/src/auth/jwt.d.ts +43 -0
- package/dist/server-core/src/auth/linkedin-oauth.d.ts +18 -0
- package/dist/server-core/src/auth/microsoft-oauth.d.ts +16 -0
- package/dist/server-core/src/auth/middleware.d.ts +81 -0
- package/dist/server-core/src/auth/password.d.ts +22 -0
- package/dist/server-core/src/auth/rate-limiter.d.ts +31 -0
- package/dist/server-core/src/auth/routes.d.ts +27 -0
- package/dist/server-core/src/auth/slack-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/spotify-oauth.d.ts +12 -0
- package/dist/server-core/src/auth/twitter-oauth.d.ts +18 -0
- package/dist/server-core/src/bootstrappers/index.d.ts +0 -0
- package/dist/server-core/src/collections/BackendCollectionRegistry.d.ts +13 -0
- package/dist/server-core/src/collections/loader.d.ts +5 -0
- package/dist/server-core/src/cron/cron-loader.d.ts +17 -0
- package/dist/server-core/src/cron/cron-routes.d.ts +14 -0
- package/dist/server-core/src/cron/cron-scheduler.d.ts +106 -0
- package/dist/server-core/src/cron/cron-store.d.ts +32 -0
- package/dist/server-core/src/cron/index.d.ts +6 -0
- package/dist/server-core/src/db/interfaces.d.ts +18 -0
- package/dist/server-core/src/email/index.d.ts +6 -0
- package/dist/server-core/src/email/smtp-email-service.d.ts +25 -0
- package/dist/server-core/src/email/templates.d.ts +42 -0
- package/dist/server-core/src/email/types.d.ts +107 -0
- package/dist/server-core/src/functions/function-loader.d.ts +17 -0
- package/dist/server-core/src/functions/function-routes.d.ts +10 -0
- package/dist/server-core/src/functions/index.d.ts +3 -0
- package/dist/server-core/src/history/history-routes.d.ts +23 -0
- package/dist/server-core/src/history/index.d.ts +1 -0
- package/dist/server-core/src/index.d.ts +29 -0
- package/dist/server-core/src/init.d.ts +168 -0
- package/dist/server-core/src/serve-spa.d.ts +30 -0
- package/dist/server-core/src/services/driver-registry.d.ts +78 -0
- package/dist/server-core/src/singleton.d.ts +35 -0
- package/dist/server-core/src/storage/LocalStorageController.d.ts +46 -0
- package/dist/server-core/src/storage/S3StorageController.d.ts +36 -0
- package/dist/server-core/src/storage/index.d.ts +25 -0
- package/dist/server-core/src/storage/routes.d.ts +38 -0
- package/dist/server-core/src/storage/storage-registry.d.ts +78 -0
- package/dist/server-core/src/storage/types.d.ts +103 -0
- package/dist/server-core/src/types/index.d.ts +11 -0
- package/dist/server-core/src/utils/dev-port.d.ts +35 -0
- package/dist/server-core/src/utils/logger.d.ts +31 -0
- package/dist/server-core/src/utils/logging.d.ts +9 -0
- package/dist/server-core/src/utils/request-logger.d.ts +19 -0
- package/dist/server-core/src/utils/sql.d.ts +27 -0
- package/dist/types/src/controllers/analytics_controller.d.ts +7 -0
- package/dist/types/src/controllers/auth.d.ts +119 -0
- package/dist/types/src/controllers/client.d.ts +170 -0
- package/dist/types/src/controllers/collection_registry.d.ts +46 -0
- package/dist/types/src/controllers/customization_controller.d.ts +60 -0
- package/dist/types/src/controllers/data.d.ts +168 -0
- package/dist/types/src/controllers/data_driver.d.ts +195 -0
- package/dist/types/src/controllers/database_admin.d.ts +11 -0
- package/dist/types/src/controllers/dialogs_controller.d.ts +36 -0
- package/dist/types/src/controllers/effective_role.d.ts +4 -0
- package/dist/types/src/controllers/email.d.ts +34 -0
- package/dist/types/src/controllers/index.d.ts +18 -0
- package/dist/types/src/controllers/local_config_persistence.d.ts +20 -0
- package/dist/types/src/controllers/navigation.d.ts +213 -0
- package/dist/types/src/controllers/registry.d.ts +54 -0
- package/dist/types/src/controllers/side_dialogs_controller.d.ts +67 -0
- package/dist/types/src/controllers/side_entity_controller.d.ts +90 -0
- package/dist/types/src/controllers/snackbar.d.ts +24 -0
- package/dist/types/src/controllers/storage.d.ts +171 -0
- package/dist/types/src/index.d.ts +4 -0
- package/dist/types/src/rebase_context.d.ts +105 -0
- package/dist/types/src/types/backend.d.ts +536 -0
- package/dist/types/src/types/backend_hooks.d.ts +187 -0
- package/dist/types/src/types/builders.d.ts +15 -0
- package/dist/types/src/types/chips.d.ts +5 -0
- package/dist/types/src/types/collections.d.ts +857 -0
- package/dist/types/src/types/cron.d.ts +102 -0
- package/dist/types/src/types/data_source.d.ts +64 -0
- package/dist/types/src/types/entities.d.ts +145 -0
- package/dist/types/src/types/entity_actions.d.ts +98 -0
- package/dist/types/src/types/entity_callbacks.d.ts +173 -0
- package/dist/types/src/types/entity_link_builder.d.ts +7 -0
- package/dist/types/src/types/entity_overrides.d.ts +10 -0
- package/dist/types/src/types/entity_views.d.ts +59 -0
- package/dist/types/src/types/export_import.d.ts +21 -0
- package/dist/types/src/types/formex.d.ts +40 -0
- package/dist/types/src/types/index.d.ts +25 -0
- package/dist/types/src/types/locales.d.ts +4 -0
- package/dist/types/src/types/modify_collections.d.ts +5 -0
- package/dist/types/src/types/plugins.d.ts +282 -0
- package/dist/types/src/types/properties.d.ts +1148 -0
- package/dist/types/src/types/property_config.d.ts +70 -0
- package/dist/types/src/types/relations.d.ts +336 -0
- package/dist/types/src/types/slots.d.ts +262 -0
- package/dist/types/src/types/translations.d.ts +874 -0
- package/dist/types/src/types/user_management_delegate.d.ts +121 -0
- package/dist/types/src/types/websockets.d.ts +78 -0
- package/dist/types/src/users/index.d.ts +2 -0
- package/dist/types/src/users/roles.d.ts +22 -0
- package/dist/types/src/users/user.d.ts +46 -0
- package/history_diff.log +385 -0
- package/jest.config.cjs +16 -0
- package/package.json +86 -0
- package/scratch.ts +9 -0
- package/src/api/ast-schema-editor.ts +289 -0
- package/src/api/collections_for_test/callbacks_test_collection.ts +60 -0
- package/src/api/errors.ts +179 -0
- package/src/api/graphql/graphql-schema-generator.ts +336 -0
- package/src/api/graphql/index.ts +2 -0
- package/src/api/index.ts +11 -0
- package/src/api/openapi-generator.ts +715 -0
- package/src/api/rest/api-generator-count.test.ts +113 -0
- package/src/api/rest/api-generator.ts +573 -0
- package/src/api/rest/index.ts +2 -0
- package/src/api/rest/query-parser.ts +155 -0
- package/src/api/schema-editor-routes.ts +41 -0
- package/src/api/server.ts +249 -0
- package/src/api/types.ts +90 -0
- package/src/auth/admin-routes.ts +605 -0
- package/src/auth/apple-oauth.ts +120 -0
- package/src/auth/bitbucket-oauth.ts +82 -0
- package/src/auth/discord-oauth.ts +83 -0
- package/src/auth/facebook-oauth.ts +72 -0
- package/src/auth/github-oauth.ts +110 -0
- package/src/auth/gitlab-oauth.ts +70 -0
- package/src/auth/google-oauth.ts +48 -0
- package/src/auth/index.ts +34 -0
- package/src/auth/interfaces.ts +363 -0
- package/src/auth/jwt.ts +181 -0
- package/src/auth/linkedin-oauth.ts +81 -0
- package/src/auth/microsoft-oauth.ts +88 -0
- package/src/auth/middleware.ts +384 -0
- package/src/auth/password.ts +77 -0
- package/src/auth/rate-limiter.ts +133 -0
- package/src/auth/routes.ts +788 -0
- package/src/auth/slack-oauth.ts +71 -0
- package/src/auth/spotify-oauth.ts +67 -0
- package/src/auth/twitter-oauth.ts +120 -0
- package/src/bootstrappers/index.ts +1 -0
- package/src/collections/BackendCollectionRegistry.ts +20 -0
- package/src/collections/loader.ts +49 -0
- package/src/cron/cron-loader.ts +89 -0
- package/src/cron/cron-routes.test.ts +265 -0
- package/src/cron/cron-routes.ts +85 -0
- package/src/cron/cron-scheduler.test.ts +547 -0
- package/src/cron/cron-scheduler.ts +576 -0
- package/src/cron/cron-store.ts +163 -0
- package/src/cron/index.ts +6 -0
- package/src/db/interfaces.ts +60 -0
- package/src/email/index.ts +18 -0
- package/src/email/smtp-email-service.ts +91 -0
- package/src/email/templates.ts +388 -0
- package/src/email/types.ts +105 -0
- package/src/functions/function-loader.ts +119 -0
- package/src/functions/function-routes.ts +31 -0
- package/src/functions/index.ts +3 -0
- package/src/history/history-routes.ts +129 -0
- package/src/history/index.ts +2 -0
- package/src/index.ts +66 -0
- package/src/init.ts +737 -0
- package/src/serve-spa.ts +81 -0
- package/src/services/driver-registry.ts +182 -0
- package/src/singleton.test.ts +28 -0
- package/src/singleton.ts +70 -0
- package/src/storage/LocalStorageController.ts +365 -0
- package/src/storage/S3StorageController.ts +298 -0
- package/src/storage/index.ts +43 -0
- package/src/storage/routes.ts +264 -0
- package/src/storage/storage-registry.ts +187 -0
- package/src/storage/types.ts +134 -0
- package/src/types/index.ts +27 -0
- package/src/utils/dev-port.ts +176 -0
- package/src/utils/logger.ts +143 -0
- package/src/utils/logging.ts +38 -0
- package/src/utils/request-logger.ts +66 -0
- package/src/utils/sql.ts +38 -0
- package/test/admin-routes.test.ts +640 -0
- package/test/api-generator.test.ts +501 -0
- package/test/ast-schema-editor.test.ts +63 -0
- package/test/auth-middleware-hono.test.ts +556 -0
- package/test/auth-routes.test.ts +1047 -0
- package/test/backend-hooks-admin.test.ts +394 -0
- package/test/backend-hooks-data.test.ts +408 -0
- package/test/driver-registry.test.ts +282 -0
- package/test/error-propagation.test.ts +226 -0
- package/test/errors-hono.test.ts +133 -0
- package/test/errors.test.ts +155 -0
- package/test/jwt-security.test.ts +182 -0
- package/test/jwt.test.ts +324 -0
- package/test/middleware.test.ts +300 -0
- package/test/password.test.ts +165 -0
- package/test/query-parser.test.ts +263 -0
- package/test/rate-limiter.test.ts +102 -0
- package/test/safe-compare.test.ts +66 -0
- package/test/singleton.test.ts +59 -0
- package/test/storage-local.test.ts +271 -0
- package/test/storage-registry.test.ts +282 -0
- package/test/storage-routes.test.ts +222 -0
- package/test/storage-s3.test.ts +304 -0
- package/test-ast.ts +28 -0
- package/test.ts +6 -0
- package/test_output.txt +1133 -0
- package/tsconfig.json +49 -0
- package/tsconfig.prod.json +20 -0
- package/vite.config.ts +80 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { ApiError, errorHandler } from "../src/api/errors";
|
|
3
|
+
import { HonoEnv } from "../src/api/types";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* End-to-end tests that verify ApiError thrown inside Hono sub-routers
|
|
7
|
+
* produces the correct HTTP status code and JSON body WHEN the sub-router
|
|
8
|
+
* is mounted on a parent app — exactly simulating the init.ts mounting
|
|
9
|
+
* pattern.
|
|
10
|
+
*
|
|
11
|
+
* This is critical because Hono's `onError` does NOT propagate from parent
|
|
12
|
+
* to child routers. Each sub-router must have its own `router.onError(errorHandler)`.
|
|
13
|
+
*/
|
|
14
|
+
describe("Error Propagation through Sub-Routers", () => {
|
|
15
|
+
|
|
16
|
+
// ── Without onError on sub-router (broken behavior) ─────────────
|
|
17
|
+
|
|
18
|
+
describe("Sub-router WITHOUT onError (demonstrates the bug)", () => {
|
|
19
|
+
function createBrokenApp() {
|
|
20
|
+
// This simulates the REAL production scenario: the consumer's Hono app
|
|
21
|
+
// (e.g. SustenTalent's index.ts) does NOT set onError on the parent app.
|
|
22
|
+
// Without onError on the sub-router either, Hono's DEFAULT error handler
|
|
23
|
+
// catches ApiErrors and returns a generic 500 with a plain text body.
|
|
24
|
+
const parentApp = new Hono<HonoEnv>();
|
|
25
|
+
// NOTE: no parentApp.onError(errorHandler) — this is the real scenario
|
|
26
|
+
|
|
27
|
+
const subRouter = new Hono<HonoEnv>();
|
|
28
|
+
// NOTE: no subRouter.onError(errorHandler) — this is the bug
|
|
29
|
+
subRouter.get("/login", () => {
|
|
30
|
+
throw ApiError.unauthorized("Invalid credentials", "INVALID_CREDENTIALS");
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
parentApp.route("/api/auth", subRouter);
|
|
34
|
+
return parentApp;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
it("ApiError.unauthorized returns 500 plain text without ANY onError handler", async () => {
|
|
38
|
+
const app = createBrokenApp();
|
|
39
|
+
const res = await app.request("/api/auth/login");
|
|
40
|
+
// Without any onError handler, Hono's default handler returns 500
|
|
41
|
+
expect(res.status).toBe(500);
|
|
42
|
+
// And the body is NOT our structured JSON — it's plain text
|
|
43
|
+
const text = await res.text();
|
|
44
|
+
expect(text).toBe("Internal Server Error");
|
|
45
|
+
});
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
// ── With onError on sub-router (fixed behavior) ─────────────────
|
|
49
|
+
|
|
50
|
+
describe("Sub-router WITH onError (correct behavior)", () => {
|
|
51
|
+
function createFixedApp() {
|
|
52
|
+
const parentApp = new Hono<HonoEnv>();
|
|
53
|
+
parentApp.onError(errorHandler);
|
|
54
|
+
|
|
55
|
+
const subRouter = new Hono<HonoEnv>();
|
|
56
|
+
subRouter.onError(errorHandler); // ← THE FIX
|
|
57
|
+
|
|
58
|
+
subRouter.get("/login", () => {
|
|
59
|
+
throw ApiError.unauthorized("Invalid credentials", "INVALID_CREDENTIALS");
|
|
60
|
+
});
|
|
61
|
+
subRouter.get("/register", () => {
|
|
62
|
+
throw ApiError.conflict("Email already exists", "EMAIL_EXISTS");
|
|
63
|
+
});
|
|
64
|
+
subRouter.get("/refresh", () => {
|
|
65
|
+
throw ApiError.unauthorized("Refresh token expired", "TOKEN_EXPIRED");
|
|
66
|
+
});
|
|
67
|
+
subRouter.get("/forbidden", () => {
|
|
68
|
+
throw ApiError.forbidden("Admin only");
|
|
69
|
+
});
|
|
70
|
+
subRouter.get("/not-found", () => {
|
|
71
|
+
throw ApiError.notFound("User not found");
|
|
72
|
+
});
|
|
73
|
+
subRouter.get("/bad-request", () => {
|
|
74
|
+
throw ApiError.badRequest("Missing email", "INVALID_INPUT", { field: "email" });
|
|
75
|
+
});
|
|
76
|
+
subRouter.get("/service-unavailable", () => {
|
|
77
|
+
throw ApiError.serviceUnavailable("Google login not configured", "NOT_CONFIGURED");
|
|
78
|
+
});
|
|
79
|
+
subRouter.get("/generic-error", () => {
|
|
80
|
+
throw new Error("Unexpected failure");
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
parentApp.route("/api/auth", subRouter);
|
|
84
|
+
return parentApp;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
it("returns 401 for unauthorized errors", async () => {
|
|
88
|
+
const app = createFixedApp();
|
|
89
|
+
const res = await app.request("/api/auth/login");
|
|
90
|
+
expect(res.status).toBe(401);
|
|
91
|
+
const body = await res.json() as { error: { message: string; code: string } };
|
|
92
|
+
expect(body.error.message).toBe("Invalid credentials");
|
|
93
|
+
expect(body.error.code).toBe("INVALID_CREDENTIALS");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("returns 409 for conflict errors", async () => {
|
|
97
|
+
const app = createFixedApp();
|
|
98
|
+
const res = await app.request("/api/auth/register");
|
|
99
|
+
expect(res.status).toBe(409);
|
|
100
|
+
const body = await res.json() as { error: { message: string; code: string } };
|
|
101
|
+
expect(body.error.message).toBe("Email already exists");
|
|
102
|
+
expect(body.error.code).toBe("EMAIL_EXISTS");
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("returns 401 for expired refresh token", async () => {
|
|
106
|
+
const app = createFixedApp();
|
|
107
|
+
const res = await app.request("/api/auth/refresh");
|
|
108
|
+
expect(res.status).toBe(401);
|
|
109
|
+
const body = await res.json() as { error: { message: string; code: string } };
|
|
110
|
+
expect(body.error.code).toBe("TOKEN_EXPIRED");
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("returns 403 for forbidden errors", async () => {
|
|
114
|
+
const app = createFixedApp();
|
|
115
|
+
const res = await app.request("/api/auth/forbidden");
|
|
116
|
+
expect(res.status).toBe(403);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
it("returns 404 for not found errors", async () => {
|
|
120
|
+
const app = createFixedApp();
|
|
121
|
+
const res = await app.request("/api/auth/not-found");
|
|
122
|
+
expect(res.status).toBe(404);
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
it("returns 400 with details for bad request errors", async () => {
|
|
126
|
+
const app = createFixedApp();
|
|
127
|
+
const res = await app.request("/api/auth/bad-request");
|
|
128
|
+
expect(res.status).toBe(400);
|
|
129
|
+
const body = await res.json() as { error: { message: string; code: string; details: unknown } };
|
|
130
|
+
expect(body.error.code).toBe("INVALID_INPUT");
|
|
131
|
+
expect(body.error.details).toEqual({ field: "email" });
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
it("returns 503 for service unavailable errors", async () => {
|
|
135
|
+
const app = createFixedApp();
|
|
136
|
+
const res = await app.request("/api/auth/service-unavailable");
|
|
137
|
+
expect(res.status).toBe(503);
|
|
138
|
+
const body = await res.json() as { error: { message: string; code: string } };
|
|
139
|
+
expect(body.error.code).toBe("NOT_CONFIGURED");
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it("returns 500 with sanitized message for generic errors", async () => {
|
|
143
|
+
const app = createFixedApp();
|
|
144
|
+
const res = await app.request("/api/auth/generic-error");
|
|
145
|
+
expect(res.status).toBe(500);
|
|
146
|
+
const body = await res.json() as { error: { message: string; code: string } };
|
|
147
|
+
expect(body.error.code).toBe("INTERNAL_ERROR");
|
|
148
|
+
// Should NOT leak the raw error message
|
|
149
|
+
expect(body.error.message).toBe("Internal Server Error");
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it("returns consistent { error: { message, code } } shape for all error types", async () => {
|
|
153
|
+
const app = createFixedApp();
|
|
154
|
+
const paths = ["/api/auth/login", "/api/auth/register", "/api/auth/forbidden", "/api/auth/not-found", "/api/auth/bad-request"];
|
|
155
|
+
|
|
156
|
+
for (const path of paths) {
|
|
157
|
+
const res = await app.request(path);
|
|
158
|
+
const body = await res.json() as Record<string, unknown>;
|
|
159
|
+
expect(body).toHaveProperty("error");
|
|
160
|
+
expect(body.error).toHaveProperty("message");
|
|
161
|
+
expect(body.error).toHaveProperty("code");
|
|
162
|
+
}
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
// ── Multi-level nesting ─────────────────────────────────────────
|
|
167
|
+
|
|
168
|
+
describe("Deeply nested sub-routers", () => {
|
|
169
|
+
it("error handler works through two levels of nesting", async () => {
|
|
170
|
+
const root = new Hono<HonoEnv>();
|
|
171
|
+
root.onError(errorHandler);
|
|
172
|
+
|
|
173
|
+
const level1 = new Hono<HonoEnv>();
|
|
174
|
+
level1.onError(errorHandler);
|
|
175
|
+
|
|
176
|
+
const level2 = new Hono<HonoEnv>();
|
|
177
|
+
level2.onError(errorHandler);
|
|
178
|
+
|
|
179
|
+
level2.get("/deep", () => {
|
|
180
|
+
throw ApiError.unauthorized("Deep error");
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
level1.route("/nested", level2);
|
|
184
|
+
root.route("/api", level1);
|
|
185
|
+
|
|
186
|
+
const res = await root.request("/api/nested/deep");
|
|
187
|
+
expect(res.status).toBe(401);
|
|
188
|
+
const body = await res.json() as { error: { message: string } };
|
|
189
|
+
expect(body.error.message).toBe("Deep error");
|
|
190
|
+
});
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
// ── Data router simulation ──────────────────────────────────────
|
|
194
|
+
|
|
195
|
+
describe("Data router error propagation (simulates init.ts pattern)", () => {
|
|
196
|
+
function createDataApp() {
|
|
197
|
+
const app = new Hono<HonoEnv>();
|
|
198
|
+
app.onError(errorHandler);
|
|
199
|
+
|
|
200
|
+
const dataRouter = new Hono<HonoEnv>();
|
|
201
|
+
dataRouter.onError(errorHandler);
|
|
202
|
+
|
|
203
|
+
dataRouter.get("/posts/:id", () => {
|
|
204
|
+
throw ApiError.notFound("Entity not found");
|
|
205
|
+
});
|
|
206
|
+
dataRouter.post("/posts", () => {
|
|
207
|
+
throw ApiError.badRequest("Validation failed", "INVALID_INPUT");
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
app.route("/api/data", dataRouter);
|
|
211
|
+
return app;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
it("returns 404 for entity not found", async () => {
|
|
215
|
+
const app = createDataApp();
|
|
216
|
+
const res = await app.request("/api/data/posts/999");
|
|
217
|
+
expect(res.status).toBe(404);
|
|
218
|
+
});
|
|
219
|
+
|
|
220
|
+
it("returns 400 for validation errors", async () => {
|
|
221
|
+
const app = createDataApp();
|
|
222
|
+
const res = await app.request("/api/data/posts", { method: "POST" });
|
|
223
|
+
expect(res.status).toBe(400);
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
});
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import { Hono } from "hono";
|
|
2
|
+
import { ApiError, errorHandler } from "../src/api/errors";
|
|
3
|
+
import { HonoEnv } from "../src/api/types";
|
|
4
|
+
|
|
5
|
+
describe("Error Handler (Hono)", () => {
|
|
6
|
+
function createApp() {
|
|
7
|
+
const app = new Hono<HonoEnv>();
|
|
8
|
+
app.onError(errorHandler);
|
|
9
|
+
|
|
10
|
+
// Test routes that throw different errors
|
|
11
|
+
app.get("/bad-request", () => {
|
|
12
|
+
throw ApiError.badRequest("Missing required field", "MISSING_FIELD", { field: "email" });
|
|
13
|
+
});
|
|
14
|
+
app.get("/unauthorized", () => {
|
|
15
|
+
throw ApiError.unauthorized("Token expired", "TOKEN_EXPIRED");
|
|
16
|
+
});
|
|
17
|
+
app.get("/forbidden", () => {
|
|
18
|
+
throw ApiError.forbidden("Admin only", "FORBIDDEN");
|
|
19
|
+
});
|
|
20
|
+
app.get("/not-found", () => {
|
|
21
|
+
throw ApiError.notFound("Entity not found");
|
|
22
|
+
});
|
|
23
|
+
app.get("/conflict", () => {
|
|
24
|
+
throw ApiError.conflict("Email already exists", "EMAIL_EXISTS");
|
|
25
|
+
});
|
|
26
|
+
app.get("/internal", () => {
|
|
27
|
+
throw ApiError.internal("Database connection failed");
|
|
28
|
+
});
|
|
29
|
+
app.get("/service-unavailable", () => {
|
|
30
|
+
throw ApiError.serviceUnavailable("Feature not configured");
|
|
31
|
+
});
|
|
32
|
+
app.get("/generic-error", () => {
|
|
33
|
+
throw new Error("Something went wrong");
|
|
34
|
+
});
|
|
35
|
+
app.get("/error-with-code", () => {
|
|
36
|
+
const err = new Error("Rate limited") as Error & { code: string };
|
|
37
|
+
err.code = "RATE_LIMITED";
|
|
38
|
+
throw err;
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
return app;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
it("formats ApiError with correct status and body structure", async () => {
|
|
45
|
+
const app = createApp();
|
|
46
|
+
const res = await app.request("/bad-request");
|
|
47
|
+
expect(res.status).toBe(400);
|
|
48
|
+
const body = await res.json() as any;
|
|
49
|
+
expect(body.error.message).toBe("Missing required field");
|
|
50
|
+
expect(body.error.code).toBe("MISSING_FIELD");
|
|
51
|
+
expect(body.error.details).toEqual({ field: "email" });
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("handles 401 Unauthorized", async () => {
|
|
55
|
+
const app = createApp();
|
|
56
|
+
const res = await app.request("/unauthorized");
|
|
57
|
+
expect(res.status).toBe(401);
|
|
58
|
+
const body = await res.json() as any;
|
|
59
|
+
expect(body.error.code).toBe("TOKEN_EXPIRED");
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("handles 403 Forbidden", async () => {
|
|
63
|
+
const app = createApp();
|
|
64
|
+
const res = await app.request("/forbidden");
|
|
65
|
+
expect(res.status).toBe(403);
|
|
66
|
+
const body = await res.json() as any;
|
|
67
|
+
expect(body.error.code).toBe("FORBIDDEN");
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it("handles 404 Not Found", async () => {
|
|
71
|
+
const app = createApp();
|
|
72
|
+
const res = await app.request("/not-found");
|
|
73
|
+
expect(res.status).toBe(404);
|
|
74
|
+
const body = await res.json() as any;
|
|
75
|
+
expect(body.error.code).toBe("NOT_FOUND");
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it("handles 409 Conflict", async () => {
|
|
79
|
+
const app = createApp();
|
|
80
|
+
const res = await app.request("/conflict");
|
|
81
|
+
expect(res.status).toBe(409);
|
|
82
|
+
const body = await res.json() as any;
|
|
83
|
+
expect(body.error.code).toBe("EMAIL_EXISTS");
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
it("handles 500 Internal", async () => {
|
|
87
|
+
const app = createApp();
|
|
88
|
+
const res = await app.request("/internal");
|
|
89
|
+
expect(res.status).toBe(500);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it("handles 503 Service Unavailable", async () => {
|
|
93
|
+
const app = createApp();
|
|
94
|
+
const res = await app.request("/service-unavailable");
|
|
95
|
+
expect(res.status).toBe(503);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it("converts generic Error to 500 with INTERNAL_ERROR code", async () => {
|
|
99
|
+
const app = createApp();
|
|
100
|
+
const res = await app.request("/generic-error");
|
|
101
|
+
expect(res.status).toBe(500);
|
|
102
|
+
const body = await res.json() as any;
|
|
103
|
+
expect(body.error.code).toBe("INTERNAL_ERROR");
|
|
104
|
+
expect(body.error.message).toBe("Internal Server Error");
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
it("maps known error codes to HTTP status codes", async () => {
|
|
108
|
+
const app = createApp();
|
|
109
|
+
const res = await app.request("/error-with-code");
|
|
110
|
+
// RATE_LIMITED is not in the code-to-status map, so it should default to 500
|
|
111
|
+
expect(res.status).toBe(500);
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
it("omits details when not provided", async () => {
|
|
115
|
+
const app = createApp();
|
|
116
|
+
const res = await app.request("/unauthorized");
|
|
117
|
+
const body = await res.json() as any;
|
|
118
|
+
expect(body.error.details).toBeUndefined();
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("returns consistent error shape for all error types", async () => {
|
|
122
|
+
const app = createApp();
|
|
123
|
+
const paths = ["/bad-request", "/unauthorized", "/forbidden", "/not-found", "/internal", "/generic-error"];
|
|
124
|
+
|
|
125
|
+
for (const path of paths) {
|
|
126
|
+
const res = await app.request(path);
|
|
127
|
+
const body = await res.json() as any;
|
|
128
|
+
expect(body).toHaveProperty("error");
|
|
129
|
+
expect(body.error).toHaveProperty("message");
|
|
130
|
+
expect(body.error).toHaveProperty("code");
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
});
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { ApiError, errorHandler } from "../src/api/errors";
|
|
2
|
+
|
|
3
|
+
// ── Minimal Hono-context mock ────────────────────────────────────────────
|
|
4
|
+
function createMockContext(method = "GET", path = "/test") {
|
|
5
|
+
let capturedStatus: number | undefined;
|
|
6
|
+
let capturedBody: any;
|
|
7
|
+
|
|
8
|
+
const c = {
|
|
9
|
+
req: { method,
|
|
10
|
+
path },
|
|
11
|
+
json: (body: any, status?: number) => {
|
|
12
|
+
capturedBody = body;
|
|
13
|
+
capturedStatus = status ?? 200;
|
|
14
|
+
return new Response(JSON.stringify(body), { status: capturedStatus });
|
|
15
|
+
}
|
|
16
|
+
} as any;
|
|
17
|
+
|
|
18
|
+
return {
|
|
19
|
+
c,
|
|
20
|
+
getStatus: () => capturedStatus,
|
|
21
|
+
getBody: () => capturedBody
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// ── ApiError class ────────────────────────────────────────────────────────
|
|
26
|
+
describe("ApiError", () => {
|
|
27
|
+
describe("constructor", () => {
|
|
28
|
+
it("should create an error with statusCode, code, message, and details", () => {
|
|
29
|
+
const err = new ApiError(422, "VALIDATION_ERROR", "Invalid field", { field: "email" });
|
|
30
|
+
expect(err).toBeInstanceOf(Error);
|
|
31
|
+
expect(err).toBeInstanceOf(ApiError);
|
|
32
|
+
expect(err.statusCode).toBe(422);
|
|
33
|
+
expect(err.code).toBe("VALIDATION_ERROR");
|
|
34
|
+
expect(err.message).toBe("Invalid field");
|
|
35
|
+
expect(err.details).toEqual({ field: "email" });
|
|
36
|
+
expect(err.name).toBe("ApiError");
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
it("should default details to undefined", () => {
|
|
40
|
+
const err = new ApiError(400, "BAD_REQUEST", "Bad");
|
|
41
|
+
expect(err.details).toBeUndefined();
|
|
42
|
+
});
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe("factory methods", () => {
|
|
46
|
+
it("badRequest → 400", () => {
|
|
47
|
+
const err = ApiError.badRequest("Missing field", "MISSING_FIELD");
|
|
48
|
+
expect(err.statusCode).toBe(400);
|
|
49
|
+
expect(err.code).toBe("MISSING_FIELD");
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it("badRequest uses default code", () => {
|
|
53
|
+
const err = ApiError.badRequest("Oops");
|
|
54
|
+
expect(err.code).toBe("BAD_REQUEST");
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("unauthorized → 401", () => {
|
|
58
|
+
const err = ApiError.unauthorized("Bad token");
|
|
59
|
+
expect(err.statusCode).toBe(401);
|
|
60
|
+
expect(err.code).toBe("UNAUTHORIZED");
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("forbidden → 403", () => {
|
|
64
|
+
const err = ApiError.forbidden("No access");
|
|
65
|
+
expect(err.statusCode).toBe(403);
|
|
66
|
+
expect(err.code).toBe("FORBIDDEN");
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it("notFound → 404", () => {
|
|
70
|
+
const err = ApiError.notFound("Entity not found");
|
|
71
|
+
expect(err.statusCode).toBe(404);
|
|
72
|
+
expect(err.code).toBe("NOT_FOUND");
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("conflict → 409", () => {
|
|
76
|
+
const err = ApiError.conflict("Already exists", "EMAIL_EXISTS");
|
|
77
|
+
expect(err.statusCode).toBe(409);
|
|
78
|
+
expect(err.code).toBe("EMAIL_EXISTS");
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it("internal → 500", () => {
|
|
82
|
+
const err = ApiError.internal("Boom");
|
|
83
|
+
expect(err.statusCode).toBe(500);
|
|
84
|
+
expect(err.code).toBe("INTERNAL_ERROR");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("serviceUnavailable → 503", () => {
|
|
88
|
+
const err = ApiError.serviceUnavailable("Down");
|
|
89
|
+
expect(err.statusCode).toBe(503);
|
|
90
|
+
expect(err.code).toBe("SERVICE_UNAVAILABLE");
|
|
91
|
+
});
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
// ── errorHandler (Hono ErrorHandler) ──────────────────────────────────────
|
|
96
|
+
describe("errorHandler", () => {
|
|
97
|
+
it("should format ApiError with statusCode, code, message", () => {
|
|
98
|
+
const { c, getStatus, getBody } = createMockContext();
|
|
99
|
+
const err = ApiError.notFound("User not found");
|
|
100
|
+
errorHandler(err, c);
|
|
101
|
+
|
|
102
|
+
expect(getStatus()).toBe(404);
|
|
103
|
+
expect(getBody()).toEqual({
|
|
104
|
+
error: { message: "User not found",
|
|
105
|
+
code: "NOT_FOUND" }
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
it("should include details when present", () => {
|
|
110
|
+
const { c, getBody } = createMockContext();
|
|
111
|
+
const err = ApiError.badRequest("Validation failed", "VALIDATION", { fields: ["email"] });
|
|
112
|
+
errorHandler(err, c);
|
|
113
|
+
|
|
114
|
+
expect(getBody()).toEqual({
|
|
115
|
+
error: {
|
|
116
|
+
message: "Validation failed",
|
|
117
|
+
code: "VALIDATION",
|
|
118
|
+
details: { fields: ["email"] }
|
|
119
|
+
}
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
it("should handle plain Error with code property", () => {
|
|
124
|
+
const { c, getStatus, getBody } = createMockContext();
|
|
125
|
+
const err = Object.assign(new Error("Not found"), { code: "NOT_FOUND" });
|
|
126
|
+
errorHandler(err, c);
|
|
127
|
+
|
|
128
|
+
expect(getStatus()).toBe(404);
|
|
129
|
+
expect(getBody()).toEqual({
|
|
130
|
+
error: { message: "Not found",
|
|
131
|
+
code: "NOT_FOUND" }
|
|
132
|
+
});
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
it("should default to 500 for unknown errors", () => {
|
|
136
|
+
const { c, getStatus, getBody } = createMockContext();
|
|
137
|
+
const err = new Error("Something broke");
|
|
138
|
+
errorHandler(err, c);
|
|
139
|
+
|
|
140
|
+
expect(getStatus()).toBe(500);
|
|
141
|
+
expect(getBody()).toEqual({
|
|
142
|
+
error: { message: "Internal Server Error",
|
|
143
|
+
code: "INTERNAL_ERROR" }
|
|
144
|
+
});
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
it("should use statusCode from error if present", () => {
|
|
148
|
+
const { c, getStatus } = createMockContext();
|
|
149
|
+
const err = Object.assign(new Error("Rate limited"), { statusCode: 429,
|
|
150
|
+
code: "RATE_LIMITED" });
|
|
151
|
+
errorHandler(err, c);
|
|
152
|
+
|
|
153
|
+
expect(getStatus()).toBe(429);
|
|
154
|
+
});
|
|
155
|
+
});
|