cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Announcements
Want to learn some quick and useful tips to make your day easier? Check out how Calvin uses Replay to get feedback from other teams at Dropbox here.

Dropbox API Support & Feedback

Find help with the Dropbox API from other developers.

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Re: Obj-C API v2 Error Handling

Obj-C API v2 Error Handling

billymeltdown
Explorer | Level 4
Go to solution

Hi folks, I find myself having more questions about error handling in the obj-c API v2. I keep going over the README and I am still unable to figure out where I should look for documentation on figuring out what's in a routeError and when I should look to it instead of (or in addition to) the error param in a response block.

 

For instance, create folder:

 

 

DropboxClient *client = [DropboxClientsManager authorizedClient];
if (client != nil) {
    [[client.filesRoutes createFolder:self.rootPath] 
    response:^(DBFILESFolderMetadata * _Nullable result, DBFILESCreateFolderError * _Nullable routeError, DBError * _Nullable error) {
        if (result != nil) {
            // Onward to the next step!
            [self doNextThing]
        } else {
            // Bummers? which bummers?
        }
    }];
}

 

Then I go to the documentation and lookup that route error type, DBFilesCreateFolderError, and that says it's "The CreateFolderError union." All I see there is "isPath". I don't see any methods mentioned in the error handling section of the README like isWriteError.

So, what's the process of figuring out what I should be looking for here, where do I look so that I don't have to ask here for every route? Sorry if that sounds melodramatic, I just want to do it right and understand where to look for the answers myself 🙂

 

1 Accepted Solution

Accepted Solutions

Greg-DB
Dropbox Staff
Go to solution

For each API call, you'll want to handle both the route error (for errors specific to that call) as well as the general error (for errors that can happen on any call).

 

For the route specific error for createFolder, which is DBFILESCreateFolderError, there is only the isPath case, since that error doesn't have any other scenarios. (The example in the README is for the delete call, which has a different set of possible route-specific errors, under DBFILESDeleteError.)

 

To see what is possible for any particular error, I recommend checking the Objective-C SDK documentation for that error type, e.g., DBFILESCreateFolderError, which lists just isPath. (You can look at the HTTP documenation, as you linked to, but the names won't always be exactly the same, as they get translated slightly for the SDK.)

 

So, if DBFILESCreateFolderError isPath is true, you can then access the DBFILESWriteError in it via the path field.

 

Here's a more elaborate sample for createFolder showing that:

 

        [[client.filesRoutes createFolder:@"/test/path"] response:^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *routeError, DBError *error) {
            if (result) {
                NSLog(@"%@\n", result);
            } else if (routeError) {
                NSLog(@"%@\n", routeError);
                if ([routeError isPath]) {
                    if ([routeError.path isConflict]) {
                        if ([routeError.path.conflict isFolder]) {
                            NSLog(@"Could not create folder because a folder already exists at path.");
                        } else {
                            // some other path conflict error:
                            NSLog(@"%@\n", routeError.path.conflict);
                        }
                    } else if ([routeError.path isMalformedPath]) {
                        NSLog(@"Could not create folder because the path is malformed.");
                    } else { // and so on for other errors as desired
                        // some other path error:
                        NSLog(@"%@\n", routeError.path);
                    }
                }
            } else if (error) {
                NSLog(@"%@\n", error);
            }
        }];

 

View solution in original post

3 Replies 3

billymeltdown
Explorer | Level 4
Go to solution

I should add, I've also gone over to the API docs themselves (not the obj-c ones) and then I look at the corresponding method there, /create_folder, and still I don't see information I can turn into code the way I'm understanding this. For errors it shows:

 

CreateFolderError (union)
The value will be one of the following datatypes:
path WriteError

 

So I go back to my response block, and I check to see if routeError has an isWriteError property: it does not. So then I click on WriteError in the http docs and it expands:

The value will be one of the following datatypes. New values may be introduced as our API evolves.
malformed_path String? This field is optional.
conflict WriteConflictError Couldn't write to the target path because there was something in the way.
no_write_permission Void The user doesn't have permissions to write to the target location.
insufficient_space Void The user doesn't have enough available space (bytes) to write more data.
disallowed_name Void Dropbox will not save the file or folder because of its name.

 

Okay, cool, so I go back to the readme and it says 

To properly handle union types, you should call each of the is<TAG_STATE> methods associated with the union.

Okay, so, what methods are available, how do I determine those with the info above? All I see is "isPath" ....

Greg-DB
Dropbox Staff
Go to solution

For each API call, you'll want to handle both the route error (for errors specific to that call) as well as the general error (for errors that can happen on any call).

 

For the route specific error for createFolder, which is DBFILESCreateFolderError, there is only the isPath case, since that error doesn't have any other scenarios. (The example in the README is for the delete call, which has a different set of possible route-specific errors, under DBFILESDeleteError.)

 

To see what is possible for any particular error, I recommend checking the Objective-C SDK documentation for that error type, e.g., DBFILESCreateFolderError, which lists just isPath. (You can look at the HTTP documenation, as you linked to, but the names won't always be exactly the same, as they get translated slightly for the SDK.)

 

So, if DBFILESCreateFolderError isPath is true, you can then access the DBFILESWriteError in it via the path field.

 

Here's a more elaborate sample for createFolder showing that:

 

        [[client.filesRoutes createFolder:@"/test/path"] response:^(DBFILESFolderMetadata *result, DBFILESCreateFolderError *routeError, DBError *error) {
            if (result) {
                NSLog(@"%@\n", result);
            } else if (routeError) {
                NSLog(@"%@\n", routeError);
                if ([routeError isPath]) {
                    if ([routeError.path isConflict]) {
                        if ([routeError.path.conflict isFolder]) {
                            NSLog(@"Could not create folder because a folder already exists at path.");
                        } else {
                            // some other path conflict error:
                            NSLog(@"%@\n", routeError.path.conflict);
                        }
                    } else if ([routeError.path isMalformedPath]) {
                        NSLog(@"Could not create folder because the path is malformed.");
                    } else { // and so on for other errors as desired
                        // some other path error:
                        NSLog(@"%@\n", routeError.path);
                    }
                }
            } else if (error) {
                NSLog(@"%@\n", error);
            }
        }];

 

billymeltdown
Explorer | Level 4
Go to solution

Ohhhhh, okay I get it! Thanks, Greg!

Need more support?