The SQL SELECT statement is not designed to return an "error number" or "error message", when no rows are returned because a particular predicate was not satisfied.
(By "query fails" and "error in the query", I assume you are referring to the query not returning any rows because the predicates were not satisfied (i.e. whether any rows "matched" the WHERE clause), rather than an actual MySQL error.)
Given the example query in the question, that could be rewritten, to do this kind of check.
For example:
SELECT IF(db1.name IS NULL
,'Your user and password are incorrect.'
,IF(db2.user_id IS NULL
,'You don''t have permissions in database2.'
,''
) AS message
, db1.name
, db1.pass
, db1.user_id
, db2.datetime
, db2.user_id
FROM (SELECT 1 AS i) i
LEFT
JOIN database1 db1
ON db1.name = 'name'
AND db1.pass = 'pass'
LEFT
JOIN database2 db2
ON db1.user_id = db2.user_id
But the fact you can write such a statement does not mean this is a good approach. (There has to be some name for this anti-pattern.)