Skip to content

[fix] ctx.assert() throws specific error classes #1925

@isjs0

Description

@isjs0

Description: The JSDoc for HttpError in Koa states:

A re-export of HttpError from the http-error package. This is the error type that is thrown by ctx.assert() and ctx.throw().

However, ctx.assert() actually throws specific error class, not the HttpError class. This breaks instanceof HttpError checks in error handling.

Code to reproduce

import { HttpError } from 'koa'

const router = new Router()

router.get('/test', async (ctx) => {
  try {
    const data = null
    ctx.assert(data, 401, 'Unauthorized')
  } catch (e) {
    console.log(e instanceof HttpError) // ❌ false - should be true
  }
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions