Skip to content

Instantly share code, notes, and snippets.

@Dok11
Created May 31, 2018 08:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Dok11/4dc915471e32c9449c7421bd09382921 to your computer and use it in GitHub Desktop.
Save Dok11/4dc915471e32c9449c7421bd09382921 to your computer and use it in GitHub Desktop.
Angular ReportingErrorHandlerService (partical copy from angular/angular)
<?php
$input = file_get_contents('php://input');
$request = $input ? json_decode($input, true) : $_REQUEST;
if ($request['error']) {
$logPath = $_SERVER['DOCUMENT_ROOT'] . '/js-log-error.txt';
$date = date('Y.m.d H:i:s');
$message = $date . PHP_EOL . var_export($request, true) . PHP_EOL . PHP_EOL;
file_put_contents($logPath, $message, FILE_APPEND);
}
import {ErrorHandler, Injectable} from '@angular/core';
@Injectable()
export class ReportingErrorHandlerService extends ErrorHandler {
constructor() {
super();
}
public handleError(error: string | Error): void {
try {
super.handleError(error);
} catch (e) {
this.reportError(error);
}
this.reportError(error);
}
private reportError(error: string | Error) {
const client = new XMLHttpRequest();
client.open('POST', '/js-log-error.php');
client.setRequestHeader('Content-Type', 'text/plain;charset=UTF-8');
client.send(JSON.stringify({
error: error.toString(),
url: window.location.href,
screenSize: [window.innerWidth, window.innerHeight],
memory: performance ? performance['memory'] : false,
userAgent: navigator ? navigator.userAgent : false,
platform: navigator ? navigator.platform : false,
}));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment