Created
November 10, 2016 09:54
-
-
Save steren/9753aa84d6a3059a360f7a77a20e2999 to your computer and use it in GitHub Desktop.
Stackdriver Error Reporting on Play Framework 1.4
This file contains hidden or 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
public class Application extends Controller { | |
@Catch(value={Exception.class}) | |
public static void onException(Exception ex) { | |
StringWriter exceptionWriter = new StringWriter(); | |
ex.printStackTrace(new PrintWriter(exceptionWriter)); | |
Map<String, Object> payload = new HashMap<String, Object>(); | |
payload.put("message", exceptionWriter.toString()); | |
Map<String,String> serviceContextData = new HashMap<String, String>(); | |
serviceContextData.put("service", "randomgift"); | |
payload.put("serviceContext", serviceContextData); | |
Gson gson = new Gson(); | |
String payloadStr = gson.toJson(payload); | |
Map<String, String> headers = new HashMap<String,String>(); | |
headers.put("Content-Type", "application/json"); | |
// Report to Stackdriver Error Reporting: | |
String apikey = "<your-api-key>"; | |
String projectName = "<your-project-id>"; | |
WS.url("https://clouderrorreporting.googleapis.com/v1beta1/projects/" + projectName + "/events:report?key=" + apikey) | |
.headers(headers) | |
.body(payloadStr) | |
.post(); | |
Logger.info("Error reported"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment