mirror of
https://github.com/gnustep/libs-base.git
synced 2025-05-30 08:21:25 +00:00
NSRegularExpression: Improved Error and Exception handling to match macOS behaviour (#343)
* NSRegularExpression: Match macOS error handling * Add test cases for error handling * NSRegularExpression: Raise an NSInvalidArgumentException instead of NSInternalInconsistencyException * Check if NSRegularExpression throws an exception
This commit is contained in:
parent
ec051d1d30
commit
ac38295f64
2 changed files with 66 additions and 1 deletions
|
@ -52,6 +52,8 @@
|
|||
#import "Foundation/NSCoder.h"
|
||||
#import "Foundation/NSUserDefaults.h"
|
||||
#import "Foundation/NSNotification.h"
|
||||
#import "Foundation/FoundationErrors.h"
|
||||
#import "Foundation/NSError.h"
|
||||
|
||||
|
||||
/**
|
||||
|
@ -114,6 +116,15 @@ NSRegularExpressionOptionsToURegexpFlags(NSRegularExpressionOptions opts)
|
|||
options: (NSRegularExpressionOptions)opts
|
||||
error: (NSError**)e
|
||||
{
|
||||
// Raise an NSInvalidArgumentException to match macOS behaviour.
|
||||
if (!aPattern) {
|
||||
NSException *exp;
|
||||
|
||||
exp = [NSException exceptionWithName: NSInvalidArgumentException
|
||||
reason: @"nil argument"];
|
||||
[exp raise];
|
||||
}
|
||||
|
||||
uint32_t flags = NSRegularExpressionOptionsToURegexpFlags(opts);
|
||||
UText p = UTEXT_INITIALIZER;
|
||||
UParseError pe = {0};
|
||||
|
@ -131,7 +142,27 @@ NSRegularExpressionOptionsToURegexpFlags(NSRegularExpressionOptions opts)
|
|||
utext_close(&p);
|
||||
if (U_FAILURE(s))
|
||||
{
|
||||
// FIXME: Do something sensible with the error parameter.
|
||||
// Match macOS behaviour if the pattern is invalid.
|
||||
// Example:
|
||||
// Domain=NSCocoaErrorDomain
|
||||
// Code=2048 "The value “<PATTERN>” is invalid."
|
||||
// UserInfo={NSInvalidValue=<PATTERN>}
|
||||
if (e) {
|
||||
NSDictionary *userInfo;
|
||||
NSString *description;
|
||||
|
||||
description = [NSString stringWithFormat:
|
||||
@"The value “%@” is invalid.",
|
||||
aPattern];
|
||||
|
||||
userInfo = [NSDictionary dictionaryWithObjectsAndKeys:
|
||||
aPattern, @"NSInvalidValue", description, NSLocalizedDescriptionKey, nil];
|
||||
|
||||
*e = [NSError errorWithDomain: NSCocoaErrorDomain
|
||||
code: NSFormattingError
|
||||
userInfo: userInfo];
|
||||
}
|
||||
|
||||
[self release];
|
||||
return nil;
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue