mirror of
https://github.com/saltbo/zpan.git
synced 2026-09-01 15:49:00 +08:00
8abca2f88c
Collapse the two error conventions (string-reason `{ok:false,reason}` outcomes
and thrown domain-error classes) onto one. Usecases now produce typed `AppError`
values via factories (`notFound()`/`quotaExceeded()`/`featureBlocked()`/…);
handlers `throw result.error`; and `jsonError` (renamed from `renderError`) is the
single place that renders any error to an AIP-193 body + access-log line, in
`app.onError`/accessLog.
Why: the previous setup had a string→code mapping (`outcomeError` + the `OUTCOME`
table) living in parallel with a type→code mapping (`mapDomainError`), plus inline
`apiError(c, <status>, …)` calls that hand-wrote the status at every site — exactly
the drift that left the same `quota_exceeded` at 400 in one handler and 422 in the
rest. Now the status/reason live once, in the factory.
- Add `server/usecases/ports/app-error.ts`: `AppError` + factories. Status/reason
are baked in per factory, so no usecase or handler writes an HTTP code or a
magic-string reason. `AppError` also carries optional response headers
(`Retry-After`) via a `rateLimited()` factory.
- Delete `apiError`, `outcomeError`, the `OUTCOME` table, and the dead `ApiError`
class. The 67 inline guard/middleware `apiError` sites became `throw <factory>()`.
- Control-flow outcomes a handler branches on (not just renders) stay discriminated
reasons (e.g. `deleteObject` `not_trashed`); internal shared sub-usecases
(traffic-metering, licensing internals) keep string reasons, mapped at the boundary.
- Regenerate the Go OpenAPI client (saveShare gained a 422 response).
BREAKING CHANGE: POST /shares/{token}/objects quota rejection now returns 422
(was an inconsistent 400); every other quota path already returned 422.
Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
30 lines
1.2 KiB
TypeScript
30 lines
1.2 KiB
TypeScript
import type { ProFeature } from '@shared/types'
|
|
import type { Context } from 'hono'
|
|
import { createMiddleware } from 'hono/factory'
|
|
import { ZPAN_CLOUD_URL_DEFAULT } from '../../shared/constants'
|
|
import { hasFeature } from '../domain/licensing'
|
|
import { featureBlocked } from '../usecases/ports'
|
|
import { loadBindingState, normalizeHost } from '../usecases/site/licensing'
|
|
import { getSitePublicOrigin } from '../usecases/site/public-origin'
|
|
import type { Env } from './platform'
|
|
|
|
async function configuredPublicHost(c: Context<Env>): Promise<string | null> {
|
|
const origin = await getSitePublicOrigin(c.get('deps'))
|
|
return origin ? new URL(origin).host : null
|
|
}
|
|
|
|
export function requireFeature(name: ProFeature) {
|
|
return createMiddleware<Env>(async (c, next) => {
|
|
const cloudBaseUrl = c.get('platform').getEnv('ZPAN_CLOUD_URL') ?? ZPAN_CLOUD_URL_DEFAULT
|
|
const currentHost =
|
|
(await configuredPublicHost(c)) ?? normalizeHost(c.req.header('host')) ?? new URL(c.req.url).host
|
|
const state = await loadBindingState(c.get('deps'), { currentHost, cloudBaseUrl })
|
|
if (!hasFeature(name, state)) {
|
|
throw featureBlocked('Feature not available', {
|
|
metadata: { feature: name, upgradeUrl: '/settings/billing' },
|
|
})
|
|
}
|
|
await next()
|
|
})
|
|
}
|