Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7966355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T06:24:14+00:00 2026-06-04T06:24:14+00:00

Interesting programming puzzle… I failed to initialize error to nil, so the code below

  • 0

Interesting programming puzzle…

I failed to initialize error to nil, so the code below crashes.

    NSString *query = [NSString stringWithFormat:@"http://mysite.com/getlatest.php?a=%@", aSuiteName];
    NSURL *url = [NSURL URLWithString:query];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageNotAllowed timeoutInterval:10];
    [request setHTTPMethod:@"GET"];
    NSURLResponse *response = nil;
    NSError *error;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
    if ( !data || error != nil ) {
        NSLog(@"Error downlading %@", error); //CRASH
        return NO;
    }

My question is, how can I have my server return an error such that sendSynchronousRequest:returningResponse:error: actually receives an error, yet continues to download the data anyway? Some kind of non-fatal error which will result in error being initialized to an NSError object?

This is related to a similar conundrum where I’ve wanted to return status information without total failure before. So, although obscure, I’m interested to know if there is a technique for this.

Though this is a Cocoa question, I think the answer will be in php. Something like

<?php

    header( set 503 error here );

    return data;

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T06:24:16+00:00Added an answer on June 4, 2026 at 6:24 am

    I handle this by wrapping the data into an NSError object. In the case of a plain text response, I also use the response as a localizedDescription. I use a separate method to evaluate whether the result is successful. If the URL request itself generates an error, I also consider that to be a failure, and pass along the NSError provided by the URL request.

    This is snipped from my ServerRequest class:

    + (NSError *) errorForStatusCode:(NSInteger) statusCode
                         contentType:(NSString *) contentType
                    textEncodingName:(NSString *) textEncodingName
                                data:(NSData *) data {
    
        NSMutableDictionary *userInfo = [NSMutableDictionary dictionary];
        if ([contentType isEqualToString:@"text/plain"]) {
            [userInfo setValue:[[NSString alloc] initWithData:data encoding:[self stringEncodingForTextEncodingName:textEncodingName]]
                        forKey:NSLocalizedDescriptionKey];
        } else if ( ... ) {
            // handle other relevant types         
        }
    
        return [NSError errorWithDomain:[[self class] description]
                                   code:statusCode
                               userInfo:userInfo];
    }
    
    - (BOOL) statusCodeIndicatesSuccess:(NSInteger) statusCodeValue {
        return 201 == statusCodeValue; // Modify this based on your API
    }
    
    - (BOOL) runSynchronouslyReturningResult:(NSData **) resultPtr error:(NSError **) errorPtr {
        NSHTTPURLResponse *response = nil;
        NSError *error = nil;
        NSData *result = [NSURLConnection sendSynchronousRequest:[self request] returningResponse:&response error:&error];
        if (error) {
            if (errorPtr) *errorPtr = error;
            return NO;
        }
    
        self.statusCode = [response statusCode];
        self.MIMEType = [response MIMEType];
        self.textEncodingName = [response textEncodingName];
        if ([self statusCodeIndicatesSuccess:self.statusCode]) {
            if (resultPtr) *resultPtr = result;
            return YES;
        } else {
            if (errorPtr) *errorPtr = [ServerRequest errorForStatusCode:self.statusCode
                                                            contentType:self.MIMEType
                                                       textEncodingName:self.textEncodingName
                                                                   data:result];
            if (resultPtr) *resultPtr = result;
            return NO;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an interesting programming puzzle for you: You will be given two things:
From time to time you come across some interesting code or programming challenges, such
I just move my experience on C# into Ruby. Ruby is very interesting programming
One of the more interesting programming languages I've been stuck with lately is MediaWiki
I am currently involved in some interesting programming language research which has, up until
I want to learn some mobile stuff, and programming interesting things myself on mobile
Here is an interesting problem that I encountered in a programming competition: Problem statement:
I've implemented a simple multi-threaded HTTP server for my programming assignment a couple weeks
I came across this File parsing programming challenge today and found it quite interesting.
I've been seeing so much recently about functional programming and Clojure looks particularly interesting.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.