Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cocoa - NSError description coming from nowhere

I have this piece of code :

- (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse*)response 
{
    if ([response respondsToSelector:@selector(statusCode)]) {

        int statusCode = [((NSHTTPURLResponse*)response) statusCode];
        if (statusCode >= 400) {
            NSError* statusError = [NSError errorWithDomain:@"Server connection error" code:statusCode userInfo:nil];
            [self connection:connection didFailWithError:statusError];
        }
    }
}

- (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error 
{
NSLog(@"%@", [error localizedDescription]);
}

That gives for a missing page :

--> The operation couldn’t be completed. (Server connection error error 404.)

From where does that description (localized or not) comes from ?
I've just initialized the NSError with a code and a custom meaningless domain string...

like image 838
Oliver Avatar asked Aug 02 '11 23:08

Oliver


People also ask

What is made up of NSError object?

A string containing the error domain. The user info dictionary.

What is NSError?

NSError is a Cocoa class. An NSError object encapsulates information about an error condition in an extendable, object-oriented manner. It consists of a predefined error domain, a domain-specific error code, and a user info dictionary containing application-specific information.

How many parts of NSError object?

Each NSError object encodes three critical pieces of information: a status code , corresponding to a particular error domain , as well as additional context provided by a userInfo dictionary.


2 Answers

That error message means that your online resource cannot be found by the server.

For example: http://www.google.com/notthepageyourelookingfor.

HTTP 404 - Wikipedia

If you're asking where the error message originates, it ought to break down like this:

  • localizedDescription:

    The operation couldn't be completed ()

By default this method returns the object in the user info dictionary for the key NSLocalizedDescriptionKey. If the user info dictionary doesn’t contain a value for NSLocalizedDescriptionKey, a default string is constructed from the domain and code. NSLocalizedDescriptionKey is a localized string representation of the error that, if present, will be returned by localizedDescription. Available in Mac OS X v10.2 and later. Declared in NSError.h.

  • errWithDomain:@"Server connection error":

    Server connection error

  • code:statusCode:

    error 404

like image 131
Patrick Perini Avatar answered Sep 30 '22 09:09

Patrick Perini


The operation couldn’t be completed.

That is a standard POSIX error. Your domain and error code are just appended to the actual error message to determine the error's origin. Usually, a reverse-DNS style domain is used, like com.developer.package.

like image 40
Evan Mulawski Avatar answered Sep 30 '22 08:09

Evan Mulawski