E
E
Eka22016-07-13 01:26:12
Java
Eka2, 2016-07-13 01:26:12

How to improve application architecture?

I am learning Spring, I want to move error processing and logging into a separate class: there are a number of actions (in different controller classes), the results of which are logged - whether they were successful or not. Currently it's done like this:
- a base class that performs operations. if the operation ended unsuccessfully, then an exception is generated with the text of the error message, otherwise it is displayed in the log that the operation was successful

if  (item==null) {
throw new MyException(Message.ERROR_READING);
} 
else 
{
log.info(Message.SUCCESS_READING);
}

- static class with the text of error messages and successfully performed actions
public final class Message {
  public static final String ERROR_READING = "Ошибка получения данных";
  public static final String SUCCESS_READING = "Данные получены";
}

- the class of the exception, to which, when created, a text message for the error that has occurred is passed to the message property, the text of the error is written to the log in the exception constructor.
public class ItemException extends RuntimeException {
    static Logger log = Logger.getLogger(App.class.getName());
                public MyException(String message) {
      super(message);
      log.error (message);
    }
}

Can you please tell me how to improve the architecture of the application (or am I not thinking correctly at all):
1. Is it possible to log from the constructor (so as not to duplicate throw MyException every time with the error text and immediately after the log with the same text)?
2. How to deal with the log of a successful operation, like the error message is sewn into the error class?
3. Shall the Message class be halved with successful or unsuccessful operations?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2016-07-13
@TheKnight

1) And what prevents immediately on the spot to log an error without throwing an exception? Is he really needed there?
3) Do not log from the exception constructor. Not everyone will be able to guess to find the logger call there.
2) There is a good thing for messages - Resource Bundle. They allow at the same time to make internationalization not as difficult as it could be.
What's wrong with the standard try/catch?
If you really want to take everything out - make a banal ExceptionHandler and drop the caught and created exceptions into it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question