Skip to content

Instantly share code, notes, and snippets.

@davidwong
Last active March 21, 2019 10:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save davidwong/7248e19d8d3c27aa061ab1468a14ce15 to your computer and use it in GitHub Desktop.
Save davidwong/7248e19d8d3c27aa061ab1468a14ce15 to your computer and use it in GitHub Desktop.
Example code for blog post: Crashlytics and Android: Clean Way to Use Custom Crash Reports?
/**
* Attempt at generic non-fatal error report, modelled on Crashlytics error reporting methods.
*/
public class ErrorReport {
private String id; // could be user ID, for instance
private final Map<String, String> stateString;
private final Map<String, Boolean> stateBool;
private final Map<String, Integer> stateInt;
private final Map<String, Double> stateDouble;
private final Map<String, Float> stateFloat;
private final List<String> messages;
private Throwable exception;
public ErrorReport() {
stateString = new HashMap<>();
stateBool = new HashMap<>();
stateInt = new HashMap<>();
stateDouble = new HashMap<>();
stateFloat = new HashMap<>();
messages = new ArrayList<>();
}
public Optional<String> getId() {
return Optional.ofNullable(id);
}
public ErrorReport setId(String id) {
this.id = id;
return this;
}
public Map<String, String> getStateString() {
return Collections.unmodifiableMap(stateString);
}
public Map<String, Boolean> getStateBool() {
return Collections.unmodifiableMap(stateBool);
}
public Map<String, Integer> getStateInt() {
return Collections.unmodifiableMap(stateInt);
}
public Map<String, Double> getStateDouble() {
return Collections.unmodifiableMap(stateDouble);
}
public Map<String, Float> getStateFloat() {
return Collections.unmodifiableMap(stateFloat);
}
public ErrorReport addState(String key, String value)
{
stateString.put(key, value);
return this;
}
public ErrorReport addState(String key, Boolean value)
{
stateBool.put(key, value);
return this;
}
public ErrorReport addState(String key, Integer value)
{
stateInt.put(key, value);
return this;
}
public ErrorReport addState(String key, Double value)
{
stateDouble.put(key, value);
return this;
}
public ErrorReport addState(String key, Float value)
{
stateFloat.put(key, value);
return this;
}
public List<String> getMessages() {
return Collections.unmodifiableList(messages);
}
public ErrorReport addMessage(String message)
{
messages.add(message);
return this;
}
public ErrorReport setException(Throwable exception)
{
this.exception = exception;
return this;
}
public Optional<Throwable> getException() {
return Optional.ofNullable(exception);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment