Propagate 5xx server error to user + reload button. Closes #5

This commit is contained in:
relikd
2019-07-06 13:27:00 +02:00
parent 29a48384c7
commit 8dc95dda63
10 changed files with 88 additions and 34 deletions

View File

@@ -201,12 +201,32 @@ static BOOL _nextUpdateIsForced = NO;
+ (void)asyncRequest:(NSURLRequest*)request block:(nonnull void(^)(NSData * _Nullable data, NSError * _Nullable error, NSHTTPURLResponse *response))block {
[[[self nonCachingSession] dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;
if (error || [httpResponse statusCode] == 304)
NSInteger status = [httpResponse statusCode];
if (error || status == 304) { // 304 Not Modified
data = nil;
} else if (status >= 500 && status < 600) { // 5xx Server Error
NSString *reason = [NSString stringWithFormat:NSLocalizedString(@"Server HTTP error %ld.\n\n%@", nil),
status, [self extractReadableHTML:data]];
error = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorBadServerResponse userInfo:@{NSLocalizedDescriptionKey: reason}];
data = nil;
}
block(data, error, httpResponse); // if status == 304, data & error nil
}] resume];
}
/// Helper method to extract readable text from HTML
+ (NSString*)extractReadableHTML:(NSData*)data {
NSString *str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// replace all <tags> with (presumably) non-used character
str = [[NSRegularExpression regularExpressionWithPattern:@"<[^>]*>\\s*" options:kNilOptions error:nil]
stringByReplacingMatchesInString:str options:kNilOptions range:NSMakeRange(0, str.length) withTemplate:@"◊"];
// then replace multiple occurences of that character with a single new line
str = [[NSRegularExpression regularExpressionWithPattern:@"◊+" options:kNilOptions error:nil]
stringByReplacingMatchesInString:str options:kNilOptions range:NSMakeRange(0, str.length) withTemplate:@"\n"];
// finally trim whitespace at start and end
return [str stringByTrimmingCharactersInSet: NSCharacterSet.whitespaceAndNewlineCharacterSet];
}
#pragma mark - Download RSS Feed -
@@ -266,9 +286,9 @@ static BOOL _nextUpdateIsForced = NO;
dispatch_sync(dispatch_get_main_queue(), ^{ // sync! (thread is already in background)
chosenURL = askUser(parsedMeta);
});
if (!chosenURL || chosenURL.length == 0) {
// User canceled operation, show appropriate error message
*err = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:@{NSLocalizedDescriptionKey: NSLocalizedString(@"Operation canceled.", nil)}];
if (!chosenURL || chosenURL.length == 0) { // User canceled operation, show appropriate error message
NSString *reason = NSLocalizedString(@"Operation canceled.", nil);
*err = [NSError errorWithDomain:NSURLErrorDomain code:NSURLErrorCancelled userInfo:@{NSLocalizedDescriptionKey: reason}];
return NO;
}
[self parseFeedRequest:[self newRequestURL:chosenURL] xmlBlock:nil feedBlock:block];

View File

@@ -56,7 +56,7 @@ NS_INLINE CGFloat YFromTop(NSView *view) { return NSHeight(view.superview.frame)
// UI: Buttons
+ (NSButton*)button:(NSString*)text;
+ (NSButton*)buttonImageSquare:(nonnull NSImageName)name;
+ (NSButton*)buttonIcon:(NSImage*)img size:(CGFloat)size;
+ (NSButton*)buttonIcon:(nonnull NSImageName)name size:(CGFloat)size;
+ (NSButton*)inlineButton:(NSString*)text;
+ (NSPopUpButton*)popupButton:(CGFloat)w;
// UI: Others

View File

@@ -87,11 +87,11 @@
}
/// Create pure image button with no border.
+ (NSButton*)buttonIcon:(NSImage*)img size:(CGFloat)size {
+ (NSButton*)buttonIcon:(nonnull NSImageName)name size:(CGFloat)size {
NSButton *btn = [[NSButton alloc] initWithFrame: NSMakeRect(0, 0, size, size)];
btn.bezelStyle = NSBezelStyleRounded;
btn.bordered = NO;
btn.image = img;
btn.image = [NSImage imageNamed:name];
return btn;
}
@@ -271,6 +271,8 @@
self.toolTip = tt;
if (self.accessibilityLabel.length == 0)
self.accessibilityLabel = tt;
else
self.accessibilityValueDescription = tt;
return self;
}