diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h index 95422cf..95780f5 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.h @@ -35,10 +35,10 @@ #pragma mark - Request Functions. // Creates a request object with the given parameters. -- (nonnull NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters; +- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error; // Sends a given request to the server. When it is finished, it calls request finished, passing along the json object or nil if there is an error. -- (nonnull NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished; +- (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject * _Nullable error))requestFinished; #pragma mark - Loading Functions diff --git a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m index 18f2139..3507f88 100644 --- a/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m +++ b/MVMCore/MVMCore/LoadHandling/MVMCoreLoadHandler.m @@ -115,7 +115,7 @@ #pragma mark - Request Functions. -- (nonnull NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters { +- (nullable NSURLRequest *)requestWithParameters:(nonnull MVMCoreRequestParameters *)requestParameters error:(MVMCoreErrorObject *_Nonnull *_Nonnull)error { NSURL *url = nil; @@ -176,9 +176,19 @@ [parameters setObject:requestParameters.parameters forKey:@"RequestParams"]; } + // Ensure the parameters are valid json. + if (![NSJSONSerialization isValidJSONObject:parameters]) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + return nil; + } + // Logs the request parameters. - NSError *error = nil; - NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&error]; + NSError *jsonError = nil; + NSData *data = [NSJSONSerialization dataWithJSONObject:parameters options:NSJSONWritingPrettyPrinted error:&jsonError]; + if (!data) { + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:[MVMCoreGetterUtility hardcodedStringWithKey:HardcodedErrorUnableToProcess] messageToLog:jsonError.localizedDescription code:jsonError.code domain:ErrorDomainSystem location:[NSString stringWithFormat:@"requestWithParameters:pageType_%@:parameters_%@",requestParameters.pageType,parameters.description]]; + return nil; + } NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; MVMCoreLog(@"Request Parameters for URL %@:\n%@",[request.URL absoluteString], jsonString); @@ -206,9 +216,8 @@ if (body) { [request setHTTPBody:body]; } else { - // Log the json error - MVMCoreErrorObject *errorObject = [[MVMCoreErrorObject alloc] initWithTitle:nil message:nil code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters_%@",requestParameters.pageType]]; - [MVMCoreLoggingHandler addErrorToLog:errorObject]; + *error = [[MVMCoreErrorObject alloc] initWithTitle:nil message:nil code:ErrorCodeParsingJSON domain:ErrorDomainNative location:[NSString stringWithFormat:@"requestWithParameters_%@",requestParameters.pageType]]; + return nil; } return request; @@ -280,7 +289,7 @@ return body; } -- (nonnull NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished { +- (nullable NSURLSessionTask *)sendRequest:(nonnull MVMCoreRequestParameters *)requestParameters locationForError:(nonnull NSString *)locationForError requestFinished:(nullable void (^)(id _Nullable jsonObject, MVMCoreErrorObject *_Nullable error))requestFinished { #if ENABLE_HARD_CODED_RESPONSE NSDictionary *response = [[MFHardCodedServerResponse sharedInstance] getHardCodedResponseForRequest:requestParameters]; @@ -291,9 +300,16 @@ return nil; } #endif - + + MVMCoreErrorObject *error = nil; NSTimeInterval startTime = [NSDate timeIntervalSinceReferenceDate]; - NSURLRequest *request = [self requestWithParameters:requestParameters]; + NSURLRequest *request = [self requestWithParameters:requestParameters error:&error]; + if (!request) { + if (requestFinished) { + requestFinished(nil,error); + } + return nil; + } NSURLSession *session = [MVMCoreSessionObject sharedGlobal].session; if ([[MFFreebeeHandler sharedHandler] isFreeBeeEnabled]