B
B
BonBon Slick2018-02-11 16:56:49
Software design
BonBon Slick, 2018-02-11 16:56:49

Exception or new validator?

I saw it in the code where 15+ exceptions are caught, it looks very crooked and cumbersome, because you can write the same validator and return errors from it.
So what then and when is it more correct to use?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey Zimoglyadov, 2018-02-11
@wppdevelop

An exception is needed if the program cannot be continued. For example, if you need to get data from the database, but the connection to the database is not established. In this case, we call an exception, log the error and exit the program.
A validator is needed to validate data, such as input data, when you need to fill out some kind of form that has required fields. In this case, it is enough to return an array/object with errors and terminate the program. Validation errors are not program errors.

G
Gogeo, 2018-02-14
@Gogeo

Download GodValidator, I saw it somewhere on github. They can validate everything, connections to the database, data entry errors, indentation in the code. But I remember that it was not recommended to run self-validation, it always ends with an error.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question