All files / Bandstand/src/lib route.ts

100% Statements 12/12
100% Branches 5/5
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18          1x 17x 17x 17x 43x 43x 43x 3x 1x 1x 43x 17x  
/**
 * Wrap an API route handler so the guards' thrown Responses (401/403 from
 * requireSession/requireAdmin) are returned as-is. Next.js turns anything a
 * handler throws into a 500, which is wrong and looks like an outage.
 */
export function route<A extends unknown[]>(
  handler: (...args: A) => Promise<Response>,
) {
  return async (...args: A): Promise<Response> => {
    try {
      return await handler(...args)
    } catch (e) {
      if (e instanceof Response) return e
      throw e
    }
  }
}