AppKit: Return nil asset catalog when static file lacks asset table

Prevent unhandled exceptions from propagating to callers of +colorNamed: and +imageNamed: when a bundle specifies an absent asset table in Info.plist.
Catch -staticResourceData exceptions, log a warning, and return nil.

* _CPAssetCatalog.j: Catch -staticResourceData exception in +_catalogFromBundle:, log warning, and return nil.

* CPAssetCatalogTest.j: Add test for bundles whose static file lacks an asset table.
This commit is contained in:
David Richardson committed 2026-11-09 18:14:43 -06:00
1 parent 36b43e6673
commit 14fac86da8
2 files changed
+30 -2

No files matched your search

+11 -2
View File
@@ -400,9 +400,18 @@ function _CPAssetDisplayGamut()
if (!resourceName)
return nil;
var data = [[CPURL URLWithString:[aBundle pathForResource:resourceName]] staticResourceData];
var data = nil;
if (!data)
/*
A bundle whose static file lacks the table raised an exception into the caller of
+colorNamed: or +imageNamed:, since -staticResourceData throws for a resource that the
static file does not hold; it never returns nil. The bundle now has no catalog instead.
*/
try
{
data = [[CPURL URLWithString:[aBundle pathForResource:resourceName]] staticResourceData];
}
catch (anException)
{
CPLog.warn("Asset catalog: bundle " + [aBundle bundlePath] + " names \"" + resourceName + "\", which is not in its static file");
return nil;
+19
View File
@@ -79,6 +79,18 @@ function CPAssetCatalogTestContents()
};
}
/*
A bundle whose Info.plist names an asset table that its static file does not hold.
*/
@implementation CPAssetCatalogTestUnbuiltBundle : CPBundle
- (id)objectForInfoDictionaryKey:(CPString)aKey
{
return aKey === @"CPAssetCatalog" ? @"Assets.json" : nil;
}
@end
@implementation CPAssetCatalogTest : OJTestCase
/*
@@ -133,6 +145,13 @@ function CPAssetCatalogTestContents()
[self assertNull:[CPColor colorNamed:@"Background" bundle:[CPBundle bundleWithPath:@"CPAssetCatalogTestMissing.bundle"]]];
}
- (void)testABundleWhoseStaticFileLacksTheTableGivesNoColor
{
var bundle = [[CPAssetCatalogTestUnbuiltBundle alloc] initWithPath:@"CPAssetCatalogTestUnbuilt.bundle"];
[self assertNull:[CPColor colorNamed:@"Background" bundle:bundle]];
}
- (void)testAnAssetColorTakesTheEntryForTheDrawingAppearance
{
var color = [CPColor colorNamed:@"Background"];