Giter Site home page Giter Site logo

Skip process.exit(1) in Browser mode about envalid HOT 5 CLOSED

af avatar af commented on June 24, 2024
Skip process.exit(1) in Browser mode

from envalid.

Comments (5)

SimenB avatar SimenB commented on June 24, 2024 1

Is it not enough to just provide a custom reporter? https://github.com/af/envalid/blob/ad2f612ddef8b4a07d6b6d553f33aad21cbfb849/README.md#error-reporting

We could consider just throwing as well. Default behavior would then for node to print and exit with status 1, which is essentially what we do already

from envalid.

af avatar af commented on June 24, 2024

Yeah, browser usage was never really intended for this library initially, which is why node apis like process.exit() are used in various places. I'm thinking we could add a new option like onExit, which would allow the consuming code to override this default exit behavior. Would that work for your case?

from envalid.

Kim-Andersen avatar Kim-Andersen commented on June 24, 2024

Yeah, browser usage was never really intended for this library initially, which is why node apis like process.exit() are used in various places. I'm thinking we could add a new option like onExit, which would allow the consuming code to override this default exit behavior. Would that work for your case?

Yes, onExit would do the job.

from envalid.

af avatar af commented on June 24, 2024

Ah! Thank you @SimenB, I forgot process.exit() is only used in the reporter. That's definitely the
way to go here, so closing this out.

from envalid.

kachkaev avatar kachkaev commented on June 24, 2024

It'd be useful to split a defaultReporter function into two: one would dump errors into an output string and another one will run console.error and process.exit.

I’ve got at least two projects where I'd be interested in importing the dump function. In those, I prefer throwing instead of exiting inside the reporter, because having a valid env is an optional thing.

@af will you be happy to accept a PR that extracts dumpErrors from defaultReporter and exports the new function as envalid.dumpErrors? Happy to work on it. I know I can just copy-paste the code and format errors as I want, but I like you that's done in the libarary, so keen to keep my apps' output style in sync with the original.


Here is my workaround for now:

const defaultReporter = require("envalid/src/reporter");

const customReporter = (opts) => {
  const originalConsoleError = console.error;
  const originalProcessExit = process.exit;

  let errorOutput = "";
  console.error = (output) => {
    errorOutput += output;
  };
  process.exit = () => {};

  defaultReporter(opts);

  process.exit = originalProcessExit;
  console.error = originalConsoleError;

  if (errorOutput) {
    throw new Error("Environment variables are not valid\n\n" + errorOutput);
  }
};

Instead, I'd like to write:

const { dumpErrors } = require "envalid";

const customReporter = (opts) => {
  const output = dumpErrors(opts)
  if (output) {
    throw new Error("Environment variables are not valid\n\n" + output);
  }
};

from envalid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.