Merge pull request #10 in BPHVB/mvm_core from bugfix/request_crash to develop

* commit 'f275d04635c377f0716a4bda598ce25bccc51358':
  remove unneeded nil check because nun null type
  Crash fix to error log instead
This commit is contained in:
Pfeil, Scott Robert 2018-03-07 18:12:13 -05:00
commit ab90be2417
2 changed files with 27 additions and 11 deletions

View File

@ -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

View File

@ -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]