• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Configurable Errors - Design Question

 
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranch hands,

I am tasked with creating a configurable errors file which gets loaded as a properties file from an init servlet:


The error.properties file looks like this:


My question to you, is what is the *BEST* design pattern to facilitate this in a POJO? I want to be able to throw these errors into my application.

Should I just be using System.getProperties("error.required.field"); place that into a String and then invoke that in the particular area which I want to display the error?

Or should, I create a class called ActionError and use it as a helper?

-JD

[ January 12, 2007: Message edited by: James Dekker ]
[ January 13, 2007: Message edited by: James Dekker ]
 
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
> Or should, I create a class called ActionError and use it as a helper?
i vote for this.
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

Thank you for responding! Can you give me an example of a design pattern or a code snippet that does this?

-JD
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It would be something like this

 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

Thank you for such great insight!

So, from your suggestion, would the client (program which uses this to display the error), look like this:



I don't understand what the Locale object in the ActionError() constructor is for?

In addition, how do I set errorMsg to display error.required.field? I don't understand where this id gets set and also the varargs Object for the getMessage() method?

You were a great help!

-JD
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
James, are you planning on throwing and catching this thing, like other Exceptions and Errors? Or is it intended to be used for something else entirely? If the former, you will probably want to extend Exception (or a subclass) and change the name from ActionError to ActionException. If the latter, you should probably still change the name - ErrorMessage maybe? The point is, it's pretty standard in Java that class names ending in Error or Exception represent sublclasses of Error or Exception, respectively. So if your code is intended to be used by other Java developers, it would be a good idea to use names that don't mislead people about how the class will be used.
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
the locale is used when you are dealing with multi-locate application. If its not needed in you case, you can very well remove it.

I would suggest to create an instance of ActionError class on the servlet or start of the method and to get the error message use
the id and the parameters. If the error.properties file looks like

missing.field = The field %s is missing.

Now, if you are checking the name field,
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

Interesting...

Some questions:

(1) Why doesn't the ActionError's constructor have a ResourceBundle in a parameter?




(2) Is the %s sort of like a dynamic variable which one can use to concatenate Strings?

Jim,

These errors will be placed into a Label object so therefore, its not like they are errors for a framework standpoint, they are intended for use during application runtime purposes.

I am not instantiating these into a servlet, I am doing this into a POJO which eventually gets rendered into DHTML, via the use of the Echo2 Framework (AJAX implementation).

Thanks for the great insight, gentlemen!

Right on,

James
[ January 13, 2007: Message edited by: James Dekker ]
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
#1 - its loading it inside
#2 - yes.
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

This is the code that I created based on your solution:



This is the init servlet:



The error.properties file looks like this:



I have my ant build script move the error.properties to this folder:



Now, when I invoke this class, through a client, this is the error that I get:



(1) Why is DynamicError having trouble finding the error.properties file?

(2) If I choose to use the LoadErrorProperties.getErrorProperties() init servlet inside DynamicError, there's a type mismatch with getErrorProperties returning a Properties type versus m_resource looking for a ResourceBundle... Meaning, how do I grab the file through my init servlet and use it inside DynamicError?

All my best,

James Dekker
[ January 16, 2007: Message edited by: James Dekker ]
 
Ajay Singh
Ranch Hand
Posts: 182
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You need to make sure that error.properties is there on the classpath
 
James Dekker
Ranch Hand
Posts: 231
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ajay,

It was m_resource = ResourceBundle.getBundle("error"); instead of m_resource = ResourceBundle.getBundle("error.properties");

Thanks again!

-JD
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic