Comments (5)
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.
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.
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 likeonExit
, 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.
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.
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)
- Help with Next.js HOT 5
- ts-expect-error problem with 7.2.1 HOT 4
- Make an optional case-insensitive flag HOT 2
- NPM package is not valid HOT 3
- Support `structuredClone` HOT 3
- How to access desc HOT 2
- Add parse options to str() HOT 1
- array validator HOT 1
- 7.3.0 breaking change? HOT 11
- Add option to load validator specs from .example.env file HOT 1
- Access value of env object by name HOT 1
- envalid in react-native for preview crashes without giving description of missing env HOT 2
- testOnly setting variable to Symbol instead of failing HOT 5
- Pick from validators HOT 2
- str({ devDefault: undefined }) generates an incorrect type HOT 2
- Require environment variables only if `NODE_ENV` is not set to `test` HOT 2
- Skipping env var validation in tests HOT 1
- Prevent process exit during tests HOT 1
- makeExactValidator is not exported HOT 1
- Combine envalid with Docker secrets
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from envalid.