Saturday, 27 December 2014

Grails : Project exceptions maintained in text document

/*
 * This function useful to create exception text documents of the project. when any exception rises it
 * creates the text document and saves that exception in that file
 */

def exceptionHandler(){
SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy")
SimpleDateFormat sdf1 = new SimpleDateFormat("dd-MM-yyyy HH.mm.ss")
def folder = new File("D:\\Gimboard\\"+sdf.format(new Date()))
if( !folder.exists())  folder.mkdirs()
def fileName = "Exception "+sdf1.format(new Date())+".txt"
def  fileStore = new File(folder,fileName)
fileStore.createNewFile()
def data = request.exception
def ln = System.getProperty('line.separator')

                // Two methods are there. for print stack trace lines. use one method for printing in doc

                // Method 1 :

fileStore.withWriter{
it << "Exception : $ln$ln              "+data+" $ln$ln"
it << "Cause : $ln$ln"
data.getCause().getProperties().each{prop->
if(prop.getKey() == "stackTrace"){
it << "              "+prop.getKey()+" :  "
prop.getValue().each {stack->
it << "                            "+stack+"$ln$ln"
}
}
else it << "              "+prop.getKey()+" : "+prop.getValue().toString()+" $ln$ln"
}
//it << "Trace : $ln$ln              "+data.getStackTrace().toString()+"$ln$ln"
}

// Method 2 :

fileStore.withWriter{ request.exception.stackTraceLines.each{e -> it << e } }

render view:"/error", model:[cause:data.getCause().getProperties().toString(),exception:data]
}
}

No comments:

Post a Comment