
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
@tshttp/error
Advanced tools
@tshttp/error
💢The ultimate HTTP error to throw
.
Built with a simple and discoverable API surface, highly typed, with augmentation in mind.
Requires at least typescript 4.0.
yarn add @tshttp/error
npm install @tshttp/error
import { HttpError } from '@tshttp/error'
throw new HttpError(400)
throw new HttpError(500, 'My bad')
status
The resulting error has a status
property that will be used, for example by Express and Fastify error handlers, to properly set the HTTP response status.
name
The resulting error name
property will be inferred from status
: e.g. "NotFound" for 404.
If you use a unknown error status code, the error name
will be "HttpError".
Have a look at the Augmentations section.
To respect the inherited Error
, non-enumerable properties are kept non-enumerable: name
, message
, stack
.
They won't be serialized by JSON.stringify
(MDN reference).
Instead of passing the status code, the known HTTP errors are exposed as static methods: List of HTTP errors.
throw HttpError.Unauthorized('Get out')
// HttpError { status: 400, name: 'Unauthorized', message: 'Get out' }
throw HttpError.InternalServerError('My bad')
// HttpError { status: 500, name: 'InternalServerError', message: 'My bad' }
new
keywordYou can instantiate HttpError
with or without the new
keyword, just like the built-in Error
constructor.
throw HttpError(401)
The compiler is okay with both. 👌
By default, the only parameter you can pass besides the status code is message?: string
. You might want your error objects to have more details.
A dedicated namespace Augmentation
gives the possibility, for each different status, to change the optional message
parameter to a required data
object parameter. This object's properties will be attached to the resulting error (at runtime and compile time).
export {} // necessary to be in a module file
declare module '@tshttp/error' {
namespace Augmentation {
interface Forbidden {
access: 'read' | 'create' | 'update' | 'delete'
target: string
}
// Could be useful for custom headers since frameworks usually set the response headers with the error headers property:
interface MethodNotAllowed {
headers: {
allow: ('GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE')[]
}
}
}
}
throw HttpError(403, { access: 'read', target: 'user' })
// HttpError { status: 400, name: 'Forbidden', access: 'read', target: 'user' }
throw HttpError.MethodNotAllowed({ headers: { allow: ['GET'] } })
// HttpError { status: 505, name: 'MethodNotAllowed', headers: { allow: ['GET'] } }
Every known HTTP error is available for augmentation under its own name: List of HTTP errors.
If you want to augment every HTTP error at once, use the AnyHttpError
interface.
With the Constraint
interface, you can:
declare module '@tshttp/error' {
namespace Augmentation {
interface Constraint {
status: 400 | 401 | 403 | 404 | 405 | 422 | 500
// or widen to all numbers with `status: number`
}
}
}
declare module '@tshttp/error' {
namespace Augmentation {
interface Constraint {
constructor: false
}
}
}
HttpError(400) // Compile time error
HttpError.BadRequest() // Ok
message
property stringificationIf you define a message
property with different type than string
, like so:
declare module '@tshttp/error' {
namespace Augmentation {
interface AnyHttpError {
message: Record<string, any>
}
}
}
it will always be stringified to respect the original Error
interface (at runtime and compile time).
So:
throw HttpError(400, { message: { about: 'thing' } })
gives the following stack trace:
BadRequest: {"about":"thing"} # instead of "BadRequest: [Object object]"
at ...
Since these augmentations affect the error object itself, you cannot define the following properties:
name
, status
, stack
, __proto__
, constructor
, prototype
.
Status | Name |
---|---|
400 | BadRequest |
401 | Unauthorized |
402 | PaymentRequired |
403 | Forbidden |
404 | NotFound |
405 | MethodNotAllowed |
406 | NotAcceptable |
407 | ProxyAuthenticationRequired |
408 | RequestTimeout |
409 | Conflict |
410 | Gone |
411 | LengthRequired |
412 | PreconditionFailed |
413 | PayloadTooLarge |
414 | URITooLong |
415 | UnsupportedMediaType |
416 | RequestedRangeNotSatisfiable |
417 | ExpectationFailed |
418 | ImATeapot |
421 | MisdirectedRequest |
422 | UnprocessableEntity |
423 | Locked |
424 | FailedDependency |
425 | UnorderedCollection |
426 | UpgradeRequired |
428 | PreconditionRequired |
429 | TooManyRequests |
431 | RequestHeaderFieldsTooLarge |
451 | UnavailableForLegalReasons |
500 | InternalServerError |
501 | NotImplemented |
502 | BadGateway |
503 | ServiceUnavailable |
504 | GatewayTimeout |
505 | HTTPVersionNotSupported |
506 | VariantAlsoNegotiates |
507 | InsufficientStorage |
508 | LoopDetected |
509 | BandwidthLimitExceeded |
510 | NotExtended |
511 | NetworkAuthenticationRequired |
FAQs
The ultimate HTTP Error to throw.
We found that @tshttp/error demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.