Back-end error handling?

  node.js, question

For example, I wrote a function for others to use


But when he used it, he forgot to pass it on


This is not what I expected.
So what should I do?
1. Document agreement. The contract must pass the correct parameters.
I am herescanInside threw it wrong.
3. Fault tolerant, even if he didn’t pass the entry, I will try my best to keep the program from making mistakes.

It’s very complicated. Which is the best plan?

The following details will be described again:
reqThat is, http request, if this parameter is not passed, thenscanIt doesn’t make any sense, so how should it be handled properly?

It depends on the requirements of this function.
-If this function can set default parameters, such as the default current time of creation time, this is of course the best.
-If you must pass in parameters, such as opening a file, if you set a default value to open an address that the client does not know, serious errors will occur instead. At this time, there are two methods: if there is only one error and the function has a return value, null; can be returned; If the function is void or the client needs to distinguish between different types, then different types of exceptions should be thrown.