Reuse APIError
type in server code
This commit is contained in:
parent
3b243d0fcf
commit
24331d5b88
|
@ -3,13 +3,14 @@ import { logger } from 'firebase-functions/v2'
|
||||||
import { HttpsOptions, onRequest, Request } from 'firebase-functions/v2/https'
|
import { HttpsOptions, onRequest, Request } from 'firebase-functions/v2/https'
|
||||||
import { log } from './utils'
|
import { log } from './utils'
|
||||||
import { z } from 'zod'
|
import { z } from 'zod'
|
||||||
|
import { APIError } from '../../common/api'
|
||||||
import { PrivateUser } from '../../common/user'
|
import { PrivateUser } from '../../common/user'
|
||||||
import {
|
import {
|
||||||
CORS_ORIGIN_MANIFOLD,
|
CORS_ORIGIN_MANIFOLD,
|
||||||
CORS_ORIGIN_LOCALHOST,
|
CORS_ORIGIN_LOCALHOST,
|
||||||
CORS_ORIGIN_VERCEL,
|
CORS_ORIGIN_VERCEL,
|
||||||
} from '../../common/envs/constants'
|
} from '../../common/envs/constants'
|
||||||
|
export { APIError } from '../../common/api'
|
||||||
|
|
||||||
type Output = Record<string, unknown>
|
type Output = Record<string, unknown>
|
||||||
type AuthedUser = {
|
type AuthedUser = {
|
||||||
|
@ -21,17 +22,6 @@ type JwtCredentials = { kind: 'jwt'; data: admin.auth.DecodedIdToken }
|
||||||
type KeyCredentials = { kind: 'key'; data: string }
|
type KeyCredentials = { kind: 'key'; data: string }
|
||||||
type Credentials = JwtCredentials | KeyCredentials
|
type Credentials = JwtCredentials | KeyCredentials
|
||||||
|
|
||||||
export class APIError {
|
|
||||||
code: number
|
|
||||||
msg: string
|
|
||||||
details: unknown
|
|
||||||
constructor(code: number, msg: string, details?: unknown) {
|
|
||||||
this.code = code
|
|
||||||
this.msg = msg
|
|
||||||
this.details = details
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const auth = admin.auth()
|
const auth = admin.auth()
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
const privateUsers = firestore.collection(
|
const privateUsers = firestore.collection(
|
||||||
|
@ -136,7 +126,7 @@ export const newEndpoint = (endpointOpts: EndpointOptions, fn: Handler) => {
|
||||||
res.status(200).json(await fn(req, authedUser))
|
res.status(200).json(await fn(req, authedUser))
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
if (e instanceof APIError) {
|
if (e instanceof APIError) {
|
||||||
const output: { [k: string]: unknown } = { message: e.msg }
|
const output: { [k: string]: unknown } = { message: e.message }
|
||||||
if (e.details != null) {
|
if (e.details != null) {
|
||||||
output.details = e.details
|
output.details = e.details
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user