Sinch error codes impossible to check the error

628 Views Asked by At

I am using Sinch verification service to verify phone numbers using SMS. I am implementing it from my backend to get more control over it(PHP). I am using this library.

I am in situation where I want to know when user entered verification code wrong or if user is out of retries. But the Sinch api is so bad that on "wrong code" error the response is like this:

{
  "errorCode": 40003,
  "message": "Invalid identity or code.",
  "reference": "A:21966b3d-8142-411b-a1e3-28304e0a0b2f_pncr/bYwwUiL841pJ2KB7A"
}

if out of retries:

{
  "id": "c05517b1-479a-47fe-b3bd-8961a6806761",
  "method": "sms",
  "status": "FAIL",
  "reason": "Invalid code"
}

Why they are not giving errorCodes on every error??

Now I wanted to do the check on my backend but I am out of ideas, this is how I was trying to handle this:

$sinch = new sinch();
$result = $sinch->verifyMobile($phone_number, $code);
$response = json_decode($result ,true);

if($response['errorCode'] == '40003'){
     return new JsonResponse('Wrong Code');
}elseif ($response['status'] == 'FAIL') {
     return new JsonResponse('Out of retries');
}

I get right response when the first thing happens but wrong when the second thing happens with error:

Undefined index: errorCode

What should I do?

0

There are 0 best solutions below