What’s a good way to extend Error in JavaScript?
In ES6: class MyError extends Error { constructor(message) { super(message); this.name=”MyError”; } } source
In ES6: class MyError extends Error { constructor(message) { super(message); this.name=”MyError”; } } source
Background: What’s an Optional? In Swift, Optional<Wrapped> is an option type: it can contain any value from the original (“Wrapped”) type, or no value at all (the special value nil). An optional value must be unwrapped before it can be used. Optional is a generic type, which means that Optional<Int> and Optional<String> are distinct types … Read more
JSON.stringify(err, Object.getOwnPropertyNames(err)) seems to work [from a comment by /u/ub3rgeek on /r/javascript] and felixfbecker’s comment below
There is an internal Bash variable called $PIPESTATUS; it’s an array that holds the exit status of each command in your last foreground pipeline of commands. <command> | tee out.txt ; test ${PIPESTATUS[0]} -eq 0 Or another alternative which also works with other shells (like zsh) would be to enable pipefail: set -o pipefail … … Read more
Every command that runs has an exit status. That check is looking at the exit status of the command that finished most recently before that line runs. If you want your script to exit when that test returns true (the previous command failed) then you put exit 1 (or whatever) inside that if block after … Read more
function isJson($string) { json_decode($string); return json_last_error() === JSON_ERROR_NONE; }
Is there a TRY CATCH command in Bash? No. Bash doesn’t have as many luxuries as one can find in many programming languages. There is no try/catch in bash; however, one can achieve similar behavior using && or ||. Using ||: if command1 fails then command2 runs as follows command1 || command2 Similarly, using &&, … Read more
If you are always expecting to find a value then throw the exception if it is missing. The exception would mean that there was a problem. If the value can be missing or present and both are valid for the application logic then return a null. More important: What do you do other places in … Read more
The difference between ‘throw new Error’ and ‘throw someObject’ in javascript is that throw new Error wraps the error passed to it in the following format − { name: ‘Error’, message: ‘String you pass in the constructor’ } The throw someObject will throw the object as is and will not allow any further code execution … Read more
Yes, ensure ensures that the code is always evaluated. That’s why it’s called ensure. So, it is equivalent to Java’s and C#’s finally. The general flow of begin/rescue/else/ensure/end looks like this: begin # something which might raise an exception rescue SomeExceptionClass => some_variable # code that deals with some exception rescue SomeOtherException => some_other_variable # … Read more