Big News: Socket raises $60M Series C at a $1B valuation to secure software supply chains for AI-driven development.Announcement
Sign In

@snomiao/die

Package Overview
Dependencies
Maintainers
1
Versions
15
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@snomiao/die

|| DIE('reason') Throws an error like PHP 's `OR DIE('REASON')`, Simple error throwing in a functional dev style.

latest
npmnpm
Version
1.7.0
Version published
Maintainers
1
Created
Source

PHPDIE aka @snomiao/die

|| DIE('reason') Throws an error like PHP's or DIE('REASON'), Simple error throwing in a functional dev.

Usage

import DIE from 'phpdie'
const token = process.env.TOKEN ?? DIE("Missing Token")

console.log(token) // typeof token === "string"


DIES Function

The DIES function allows you to call an alert function (like alert, console.error, toast.error, etc.) and then throw an error. This is useful for showing error messages to users before terminating execution.

import { DIES } from '@snomiao/die'

const ooops = false

// With alert
ooops || DIES(alert, "Something went wrong!");

// With console.error
ooops || DIES(console.error, "Error:", 404, "Not Found");

// With toast notifications
ooops || DIES(toast.error, "Failed to save data");

// With custom alert function
const showError = (msg) => { /* show error in UI */ };
ooops || DIES(showError, "Custom error message");

ooops || DIES(()=> 'anyway die');

The DIES function:

  • Calls the provided alert function with the given arguments
  • Throws an Error with message "DIES" and the arguments stored in the cause property
  • Never returns (return type is never)

Reference

spec

import DIE from ".";

it("lives", () => {
  const token = "123" ?? DIE("Missing Token");
  console.log(token);
  expect(token).toEqual("123");
});

it("dies", () => {
  let err: any;
  try {
    const token = process.env.TOKEN ?? DIE("Missing Token");
    console.log(token);
  } catch (e) {
    err = e;
  }
  expect(err).toEqual("Missing Token");
});

it("dies with error", () => {
  let err: any;
  try {
    const token = process.env.TOKEN ?? DIE(new Error("Missing Token"));
    console.log(token);
  } catch (e) {
    err = e;
  }
  expect(err.message).toEqual("Missing Token");
});


Impl

export function DIE(reason?: string | Error): never {
    if (typeof reason === "string") {
        const err = new Error(reason);
        throw err.stack;
    }
    throw reason;
}

Keywords

throw

FAQs

Package last updated on 18 Jul 2025

Did you know?

Socket

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.

Install

Related posts