The 502 proxy error is a known error for the ORS API matrix service, more information about it can be found here.
Suggestion to fix the bug is to detect whether the 502 proxy error happens, and re-sending the request in smaller parts, trying to avoid the error happening again.