forked from fieldexpert/fbapi4j
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds the DefaultCaseBuilder, for turning Throwable objects into bugs.
- Loading branch information
Showing
1 changed file
with
78 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package com.fieldexpert.fbapi4j; | ||
|
||
import java.io.PrintWriter; | ||
import java.io.StringWriter; | ||
|
||
import com.fieldexpert.fbapi4j.common.Attachment; | ||
|
||
public class DefaultCaseBuilder { | ||
|
||
private String project; | ||
private String area; | ||
|
||
public DefaultCaseBuilder(String project, String area) { | ||
this.project = project; | ||
this.area = area; | ||
} | ||
|
||
public Case build(Throwable t, boolean attachStackTrace) { | ||
StackTraceElement firstCause = getFirstCause(t).getStackTrace()[0]; | ||
String className = firstCause.getClassName(); | ||
String exceptionName = firstCause.getClass().getName(); | ||
String fileName = firstCause.getFileName(); | ||
int lineNumber = firstCause.getLineNumber(); | ||
|
||
String title = exceptionName + " " + className + ":" + fileName + ":" + lineNumber; | ||
StringBuilder description = new StringBuilder(); | ||
description.append(exceptionName + " \n"); | ||
description.append(className + "\n"); | ||
description.append(fileName + ":" + lineNumber + "\n"); | ||
description.append("\n"); | ||
|
||
StringWriter stackTraceWriter = new StringWriter(); | ||
PrintWriter pw = new PrintWriter(stackTraceWriter); | ||
t.printStackTrace(pw); | ||
|
||
if (!attachStackTrace) { | ||
description.append(stackTraceWriter.toString()); | ||
} | ||
|
||
Case fbCase = new Case(getProject(), getArea(), title, description.toString()); | ||
|
||
if (attachStackTrace) { | ||
Attachment attachment = new Attachment(exceptionName + ".txt","text/plain",stackTraceWriter.toString()); | ||
fbCase.attach(attachment); | ||
} | ||
|
||
return fbCase; | ||
} | ||
|
||
public Case build(Throwable t) { | ||
return build(t, true); | ||
} | ||
|
||
public String getArea() { | ||
return area; | ||
} | ||
|
||
public void setArea(String area) { | ||
this.area = area; | ||
} | ||
|
||
public String getProject() { | ||
return project; | ||
} | ||
|
||
public void setProject(String project) { | ||
this.project = project; | ||
} | ||
|
||
private static Throwable getFirstCause(Throwable e) { | ||
Throwable t = e; | ||
while (t.getCause() != null) { | ||
t = t.getCause(); | ||
} | ||
return t; | ||
} | ||
|
||
} |