4

I am working on handling error received from Parse.com while performing log in in my IOS app. I try logging in with wrong password and correct username using AFNetworking

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSDictionary *parameters = @{my parameters};
[manager POST:@"{login url}" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
    code
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Error: %@", error.localizedDescription);  <-- this line gets processed
}];

The log says

Request failed: not found (404)

I know that means wrong password, but shouldn't it be more descriptive? How do I handle other error (like Bad Request)? Shouldn't the user knows why the request failed or is a bad one?

EDIT: This is the response from Parse when I tried to login with the wrong password

{
AFNetworkingOperationFailingURLResponseErrorKey = "<NSHTTPURLResponse: 0x15571820> { URL: https://api.parse.com/1/login?password=pqpqpq&username=lalala%20namaku%20panjang%20sekali%20lhoooooooooooo } { status code: 404, headers {\n    \"Access-Control-Allow-Origin\" = \"*\";\n    \"Access-Control-Request-Method\" = \"*\";\n    Connection = \"keep-alive\";\n    \"Content-Encoding\" = gzip;\n    \"Content-Length\" = 68;\n    \"Content-Type\" = \"application/json; charset=utf-8\";\n    Date = \"Wed, 16 Jul 2014 05:49:49 GMT\";\n    Server = \"nginx/1.4.4\";\n    \"X-Parse-Platform\" = G1;\n    \"X-Runtime\" = \"0.113695\";\n} }";
NSErrorFailingURLKey = "https://api.parse.com/1/login?password=pqpqpq&username=lalala%20namaku%20panjang%20sekali%20lhoooooooooooo";
NSLocalizedDescription = "Request failed: not found (404)";
}

How do I know that this means the User object that match the username and password is not found, OR the URL (https://api.parse.com/1/login?password=pqpqpq&username=lalala%20namaku%20panjang%20sekali%20lhoooooooooooo) that is not found?

Oscar Yuandinata
  • 1,025
  • 1
  • 13
  • 31
  • 1
    Why not use native iOS SDK, where you don't care about AFNetworking and constructing and decoding URLs properly as it is done for you ? It can be installed via cocoapods quite easily ... – PetrV Jul 14 '14 at 12:59
  • It doesn't answer my question. How do I handle ambiguous error code provided by Parse.com? I mean, 404 can mean the url is not found too right? – Oscar Yuandinata Jul 15 '14 at 02:37

1 Answers1

4

Even though it returns with http code 404, the body is set in json form, like {"code":101,"error":"invalid login parameters"} ,

( values are listed for example here http://parse.com/docs/dotnet/api/html/T_Parse_ParseException_ErrorCode.htm ) . Since the code does not list exact url and how you set the headers it is hard to tell what is wrong here otherwise. You can go to https://parse.com/docs/rest#users-login and see the code example for CURL which will tell you how to call login url and it usually responds with JSON error description.

Also you can check how to get body from HTTP 404 from AFNetworking 2.0 AFHTTPSessionManager: how to get status code and response JSON in failure block?

Community
  • 1
  • 1
PetrV
  • 1,368
  • 2
  • 15
  • 30