HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaMinor

StoredErrorWriter

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
storederrorwriterstackoverflowprogramming

Problem

Functionality that overwrite standard error message with trace to user friendly message with error number stored in DB.

Not yet completed some stuff with trx...

Please review this.

DB table:

CREATE TABLE stored_error_msg
 (
id number PRIMARY KEY 
,msg VARCHAR2(2000)
,trace CLOB
,browser VARCHAR2(2000)
,date date 
,user VARCHAR2(100)
,ses_id number
 );


@ UI

this.getCurrent().setErrorHandler(new StoredErrorWriter());


StoredErrorWriter

```
package components;

import java.io.PrintWriter;
import java.io.StringWriter;

import com.vaadin.server.DefaultErrorHandler;
import com.vaadin.server.ErrorEvent;
import com.vaadin.server.ErrorHandler;
import com.vaadin.server.ErrorMessage;
import com.vaadin.server.UserError;
import com.vaadin.server.WebBrowser;
import com.vaadin.ui.AbstractComponent;
import com.vaadin.ui.UI;

import java.util.logging.Level;
import java.util.logging.Logger;

/*
* @author: Kristaps Zukovskis
* Write info about: user, date, err_msg, backtrace, browser info, ses_id
* @ DB table stored_error_msg
*/
public class StoredErrorWriter implements ErrorHandler {

private static final long serialVersionUID = 5117130906083582416L;
public static String ERROR_MSG = "System error! Error number in journal :";
private Logger logger = Logger.getAnonymousLogger();

@Override
public void error(ErrorEvent event) {

Throwable t = event.getThrowable();
if (t != null) {

String trace = getTrace(t);
final String msg = t.getMessage();

AbstractComponent component = DefaultErrorHandler
.findAbstractComponent(event);

if (component != null) {

final Integer errorNumber = InsertTrace(msg,trace);
ErrorMessage errorMessage = new UserError(ERROR_MSG
+ errorNumber);

if (errorMessage != null) {
component.setComponentError(errorMessage);
logge

Solution

Proprietary indentation convention

For some reason the first line in every method in your code is blank. This might be consistent, but since I haven't seen it anywhere else, I find it breaks the read flow, and makes your code less readable.

Use of deprecated code

Deprecation warnings are there for a reason - what they actually say is that "this may not work in next versions of this library - be warned, change this as soon as you can".

Deprecation warning also generally come with an alternative API you should use, in the case of vaadin - it suggests you use Page.getWebBrowser() instead.

Naming conventions

Generally, your method names are OK, but, for some reason, you've decided to uppercase the first letter in InsertTrace... it should start with lowercase, like the other methods.

Use static methods explicitly

To make sure your reader understand where your code lies, it is preferable that you call static methods in a fully qualified way - StoredWriterError.getTrace(t). This way it is obvious that the method is static, and it not part of the current instance.

Missing code

Since your code is missing all database-related parts, it is hard to tell whether this design is sound or not... perhaps you would like to revisit us after you complete that part.

Context

StackExchange Code Review Q#45685, answer score: 6

Revisions (0)

No revisions yet.