Created
May 31, 2018 08:38
-
-
Save Dok11/4dc915471e32c9449c7421bd09382921 to your computer and use it in GitHub Desktop.
Angular ReportingErrorHandlerService (partical copy from angular/angular)
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
<?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); | |
} |
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
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