Add documentation to the helpers module.
This commit is contained in:
@@ -63,6 +63,13 @@
|
||||
<code>src/app/_helpers/global-error-handler.ts</code>
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Description</h3>
|
||||
</p>
|
||||
<p class="comment">
|
||||
<p>A generalized http response error.</p>
|
||||
|
||||
</p>
|
||||
|
||||
<p class="comment">
|
||||
<h3>Extends</h3>
|
||||
@@ -72,6 +79,11 @@
|
||||
</p>
|
||||
|
||||
|
||||
<p class="comment">
|
||||
<h3>Example</h3>
|
||||
</p>
|
||||
<div class="io-description">
|
||||
</div>
|
||||
|
||||
<section>
|
||||
<h3 id="index">Index</h3>
|
||||
@@ -113,12 +125,14 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="8" class="link-to-prism">src/app/_helpers/global-error-handler.ts:8</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="16" class="link-to-prism">src/app/_helpers/global-error-handler.ts:16</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>Initialize the HttpError class.</p>
|
||||
</div>
|
||||
<div>
|
||||
<b>Parameters :</b>
|
||||
<table class="params">
|
||||
@@ -127,6 +141,7 @@
|
||||
<td>Name</td>
|
||||
<td>Type</td>
|
||||
<td>Optional</td>
|
||||
<td>Description</td>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
@@ -141,6 +156,12 @@
|
||||
No
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<code><ul>
|
||||
<li>The message given by the error.</li>
|
||||
</ul>
|
||||
</code>
|
||||
</td>
|
||||
</tr>
|
||||
<tr>
|
||||
<td>status</td>
|
||||
@@ -153,6 +174,12 @@
|
||||
No
|
||||
</td>
|
||||
|
||||
<td>
|
||||
<code><ul>
|
||||
<li>The status code given by the error.</li>
|
||||
</ul>
|
||||
</code>
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -189,10 +216,16 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-line">Defined in <a href="" data-line="8" class="link-to-prism">src/app/_helpers/global-error-handler.ts:8</a></div>
|
||||
<div class="io-line">Defined in <a href="" data-line="16" class="link-to-prism">src/app/_helpers/global-error-handler.ts:16</a></div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
<tr>
|
||||
<td class="col-md-4">
|
||||
<div class="io-description"><p>The error's status code. </p>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
|
||||
</tbody>
|
||||
</table>
|
||||
@@ -208,14 +241,28 @@
|
||||
|
||||
|
||||
<div class="tab-pane fade tab-source-code" id="c-source">
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import {ErrorHandler, Injectable} from '@angular/core';
|
||||
import {LoggingService} from '@app/_services/logging.service';
|
||||
import {HttpErrorResponse} from '@angular/common/http';
|
||||
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import {HttpErrorResponse} from '@angular/common/http';
|
||||
import {ErrorHandler, Injectable} from '@angular/core';
|
||||
import {Router} from '@angular/router';
|
||||
|
||||
// A generalized http response error
|
||||
// Application imports
|
||||
import {LoggingService} from '@app/_services/logging.service';
|
||||
|
||||
/**
|
||||
* A generalized http response error.
|
||||
*
|
||||
* @extends Error
|
||||
*/
|
||||
export class HttpError extends Error {
|
||||
/** The error's status code. */
|
||||
public status: number;
|
||||
|
||||
/**
|
||||
* Initialize the HttpError class.
|
||||
*
|
||||
* @param message - The message given by the error.
|
||||
* @param status - The status code given by the error.
|
||||
*/
|
||||
constructor(message: string, status: number) {
|
||||
super(message);
|
||||
this.status = status;
|
||||
@@ -223,10 +270,25 @@ export class HttpError extends Error {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a hook for centralized exception handling.
|
||||
*
|
||||
* @extends ErrorHandler
|
||||
*/
|
||||
@Injectable()
|
||||
export class GlobalErrorHandler extends ErrorHandler {
|
||||
/**
|
||||
* An array of sentence sections that denote warnings.
|
||||
* @private
|
||||
*/
|
||||
private sentencesForWarningLogging: Array<string> = [];
|
||||
|
||||
/**
|
||||
* Initialization of the Global Error Handler.
|
||||
*
|
||||
* @param loggingService - A service that provides logging capabilities.
|
||||
* @param router - A service that provides navigation among views and URL manipulation capabilities.
|
||||
*/
|
||||
constructor(
|
||||
private loggingService: LoggingService,
|
||||
private router: Router
|
||||
@@ -234,6 +296,11 @@ export class GlobalErrorHandler extends ErrorHandler {
|
||||
super();
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles different types of errors.
|
||||
*
|
||||
* @param error - An error objects thrown when a runtime errors occurs.
|
||||
*/
|
||||
handleError(error: Error): void {
|
||||
this.logError(error);
|
||||
const message: string = error.message ? error.message : error.toString();
|
||||
@@ -254,24 +321,11 @@ export class GlobalErrorHandler extends ErrorHandler {
|
||||
throw error;
|
||||
}
|
||||
|
||||
logError(error: any): void {
|
||||
const route: string = this.router.url;
|
||||
if (error instanceof HttpErrorResponse) {
|
||||
this.loggingService.sendErrorLevelMessage(
|
||||
`There was an HTTP error on route ${route}.\n${error.message}.\nStatus code: ${(error as HttpErrorResponse).status}`,
|
||||
this, {error});
|
||||
} else if (error instanceof TypeError) {
|
||||
this.loggingService.sendErrorLevelMessage(`There was a Type error on route ${route}.\n${error.message}`, this, {error});
|
||||
} else if (error instanceof Error) {
|
||||
this.loggingService.sendErrorLevelMessage(`There was a general error on route ${route}.\n${error.message}`, this, {error});
|
||||
} else {
|
||||
this.loggingService.sendErrorLevelMessage(`Nobody threw an error but something happened on route ${route}!`, this, {error});
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if an error is of type warning.
|
||||
*
|
||||
* @param errorTraceString
|
||||
* @param errorTraceString - A description of the error and it's stack trace.
|
||||
* @returns true - If the error is of type warning.
|
||||
* @private
|
||||
*/
|
||||
private isWarning(errorTraceString: string): boolean {
|
||||
@@ -288,6 +342,26 @@ export class GlobalErrorHandler extends ErrorHandler {
|
||||
|
||||
return isWarning;
|
||||
}
|
||||
|
||||
/**
|
||||
* Write appropriate logs according to the type of error.
|
||||
*
|
||||
* @param error - An error objects thrown when a runtime errors occurs.
|
||||
*/
|
||||
logError(error: any): void {
|
||||
const route: string = this.router.url;
|
||||
if (error instanceof HttpErrorResponse) {
|
||||
this.loggingService.sendErrorLevelMessage(
|
||||
`There was an HTTP error on route ${route}.\n${error.message}.\nStatus code: ${(error as HttpErrorResponse).status}`,
|
||||
this, {error});
|
||||
} else if (error instanceof TypeError) {
|
||||
this.loggingService.sendErrorLevelMessage(`There was a Type error on route ${route}.\n${error.message}`, this, {error});
|
||||
} else if (error instanceof Error) {
|
||||
this.loggingService.sendErrorLevelMessage(`There was a general error on route ${route}.\n${error.message}`, this, {error});
|
||||
} else {
|
||||
this.loggingService.sendErrorLevelMessage(`Nobody threw an error but something happened on route ${route}!`, this, {error});
|
||||
}
|
||||
}
|
||||
}
|
||||
</code></pre>
|
||||
</div>
|
||||
|
||||
Reference in New Issue
Block a user