Exception Message Service

You can implement an exception message service for resolving localized messages using error codes. The exception class uses the exception message service to load resource bundles and look up error messages based on an error code. You can use a default message provider, SUPExceptionMessageServiceImpl, or create a custom provider by implementing your own SUPExceptionMessageService.

To resolve localized messages, implement the SUPExceptionMessageService protocol.

/*!
 @protocol 
 @abstract  SUPExceptionMessageService protocol
 @discussion  SUPExceptionMessageServiceImpl is the default implementation provided for SUPExceptionMessageSerivce protocol can be registered with the SUPServiceRegistry. 
 */
@protocol SUPExceptionMessageService

/*!
 @method
 @abstract  Get the message of this error code.
 @param errorCode The error code for the message.
 @result the error message
 @discussion
 */
-(NSString*) messageWithErrorCode: (int) errorCode;

/*!
 @method
 @abstract Get the localized message of this error code for a specific locale
 @param errorCode The error code for mthe message
 @param locale locale identifier
 @result the localized message
 @discussion The locale identifier is the language-specific project (.lproj) directory name for loading resource bunlde, ErrorMessages.strings.  It could be also the value passed to NSString's initWithFormat method for string formatting the arguments.

The locale value can be in one of the following two forms:
 
- "language":  language specific value.  eg: @"en"</li>
- "language"_"region":  language and region specific value.  eg: @"en_US"
 
If the resource bundle is not found in the "language"_"region" form, The "language" part of the value is used to load the resource bundle. If a resource bundle is not found, go by [[NSBundle mainBundle] preferredLocalizations].  If it is still not found, defaults to "en".  If the value is not one of the locale identifiers available in [NSLocale availableLocaleIdentifiers],  the locale in [[NSLocale currentLocale] localeIdentifier] is used in string formatting the arguments.

 */
-(NSString*) messageWithErrorCode: (int) errorCode locale:(NSString*) locale;

@end

The exception class uses the exception message service to load resource bundles and look up error messages based on an error code.

id<SUPExceptionMessageService> provider = [[SUPServiceRegistry sharedInstance] getService:@protocol(SUPExceptionMessageService)];
NSString *message = [provider messageWithErrorCode:errorCode]; 

You can use a default message provider, SUPExceptionMessageServiceImpl. The default implementation provides a superr.bundle which contains the default English resource to look up an error message using an error code.

The SUPExceptionMessageServiceImpl loads resource bundles from the superr.bundle. You must import the superr.bundle in SUP_HOME/ObjectAPI/iOS/resources/superr.bundle to the project.

You can add support for other languages by adding new error message key-value pairs to a file named ErrorMessages.strings inside a folder named using a <language code>.lproj pattern.  The superr.bundle structure is:  

superr.bundle
  en.lproj
    ErrorMessages.strings   
  <language code>.lproj
    ErrorMessages.strings   
  <language code>.lproj
    ErrorMessages.strings   

For example, to add support for Spanish:

  1. Create a new folder, for example es.lprj, inside superr.bundle.
  2. Create a new ErrorMessage.strings text file inside the es.lprj folder.
  3. Define new localized error messages for the same set of error message keys found using the format "<error code>" = "<error message in Spanish>".
  4. Rebuild the application with the new superr.bundle file.

You can create a custom provider by implementing your own SUPExceptionMessageService.

@interface CustomMessageService : NSObject <SUPExceptionMessageService>

@end

@implementation CustomMessageService


-(NSString*) messageWithErrorCode: (int) errorCode
{
return @"my own way of retrieving the message";
}

-(NSString*) messageWithErrorCode: (int) errorCode locale:(NSString*) localName
{
return @"my own way of retrieving the localized message";
}

@end


// register our custom message provider
CustomMessageService* myProvider = [[CustomMessageService alloc] init];
[[SUPServiceRegistry sharedInstance] registerService:@protocol(SUPExceptionMessageService) withImplementation:myProvider];

See Service Registry for sample code on using the default exception message provider and how to register the default provider with the service registry.