Add documentation to the helpers module.

This commit is contained in:
Spencer Ofwiti
2021-05-11 14:34:23 +03:00
parent c9f73899c3
commit e0c045cdfa
37 changed files with 1403 additions and 585 deletions

View File

@@ -59,6 +59,13 @@
<code>src/app/_helpers/global-error-handler.ts</code>
</p>
<p class="comment">
<h3>Description</h3>
</p>
<p class="comment">
<p>Provides a hook for centralized exception handling.</p>
</p>
<p class="comment">
<h3>Extends</h3>
@@ -67,6 +74,11 @@
<code><a href="https://angular.io/api/core/ErrorHandler" target="_blank" >ErrorHandler</a></code>
</p>
<p class="comment">
<h3>Example</h3>
</p>
<div class="io-description">
</div>
<section>
<h3 id="index">Index</h3>
@@ -129,12 +141,14 @@
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="18" class="link-to-prism">src/app/_helpers/global-error-handler.ts:18</a></div>
<div class="io-line">Defined in <a href="" data-line="42" class="link-to-prism">src/app/_helpers/global-error-handler.ts:42</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Initialization of the Global Error Handler.</p>
</div>
<div>
<b>Parameters :</b>
<table class="params">
@@ -143,6 +157,7 @@
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
<tbody>
@@ -157,6 +172,12 @@
No
</td>
<td>
<code><ul>
<li>A service that provides logging capabilities.</li>
</ul>
</code>
</td>
</tr>
<tr>
<td>router</td>
@@ -169,6 +190,12 @@
No
</td>
<td>
<code><ul>
<li>A service that provides navigation among views and URL manipulation capabilities.</li>
</ul>
</code>
</td>
</tr>
</tbody>
</table>
@@ -206,14 +233,16 @@
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="27"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:27</a></div>
<div class="io-line">Defined in <a href="" data-line="62"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:62</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Handles different types of errors.</p>
</div>
<div class="io-description">
<b>Parameters :</b>
@@ -223,6 +252,7 @@
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
<tbody>
@@ -237,6 +267,12 @@
</td>
<td>
<ul>
<li>An error objects thrown when a runtime errors occurs.</li>
</ul>
</td>
</tr>
</tbody>
</table>
@@ -278,14 +314,16 @@
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="67"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:67</a></div>
<div class="io-line">Defined in <a href="" data-line="89"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:89</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Checks if an error is of type warning.</p>
</div>
<div class="io-description">
<b>Parameters :</b>
@@ -295,6 +333,7 @@
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
<tbody>
@@ -309,6 +348,12 @@
</td>
<td>
<ul>
<li>A description of the error and it&#39;s stack trace.</li>
</ul>
</td>
</tr>
</tbody>
</table>
@@ -320,7 +365,8 @@
</div>
<div class="io-description">
<p>true - If the error is of type warning.</p>
</div>
</td>
</tr>
@@ -348,14 +394,16 @@
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="47"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:47</a></div>
<div class="io-line">Defined in <a href="" data-line="109"
class="link-to-prism">src/app/_helpers/global-error-handler.ts:109</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>Write appropriate logs according to the type of error.</p>
</div>
<div class="io-description">
<b>Parameters :</b>
@@ -365,6 +413,7 @@
<td>Name</td>
<td>Type</td>
<td>Optional</td>
<td>Description</td>
</tr>
</thead>
<tbody>
@@ -379,6 +428,12 @@
</td>
<td>
<ul>
<li>An error objects thrown when a runtime errors occurs.</li>
</ul>
</td>
</tr>
</tbody>
</table>
@@ -428,10 +483,16 @@
</tr>
<tr>
<td class="col-md-4">
<div class="io-line">Defined in <a href="" data-line="18" class="link-to-prism">src/app/_helpers/global-error-handler.ts:18</a></div>
<div class="io-line">Defined in <a href="" data-line="42" class="link-to-prism">src/app/_helpers/global-error-handler.ts:42</a></div>
</td>
</tr>
<tr>
<td class="col-md-4">
<div class="io-description"><p>An array of sentence sections that denote warnings.</p>
</div>
</td>
</tr>
</tbody>
</table>
@@ -441,14 +502,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 &#x27;@angular/core&#x27;;
import {LoggingService} from &#x27;@app/_services/logging.service&#x27;;
import {HttpErrorResponse} from &#x27;@angular/common/http&#x27;;
<pre class="line-numbers compodoc-sourcecode"><code class="language-typescript">import {HttpErrorResponse} from &#x27;@angular/common/http&#x27;;
import {ErrorHandler, Injectable} from &#x27;@angular/core&#x27;;
import {Router} from &#x27;@angular/router&#x27;;
// A generalized http response error
// Application imports
import {LoggingService} from &#x27;@app/_services/logging.service&#x27;;
/**
* A generalized http response error.
*
* @extends Error
*/
export class HttpError extends Error {
/** The error&#x27;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 &#x3D; status;
@@ -456,10 +531,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&lt;string&gt; &#x3D; [];
/**
* 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
@@ -467,6 +557,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 &#x3D; error.message ? error.message : error.toString();
@@ -487,24 +582,11 @@ export class GlobalErrorHandler extends ErrorHandler {
throw error;
}
logError(error: any): void {
const route: string &#x3D; this.router.url;
if (error instanceof HttpErrorResponse) {
this.loggingService.sendErrorLevelMessage(
&#x60;There was an HTTP error on route ${route}.\n${error.message}.\nStatus code: ${(error as HttpErrorResponse).status}&#x60;,
this, {error});
} else if (error instanceof TypeError) {
this.loggingService.sendErrorLevelMessage(&#x60;There was a Type error on route ${route}.\n${error.message}&#x60;, this, {error});
} else if (error instanceof Error) {
this.loggingService.sendErrorLevelMessage(&#x60;There was a general error on route ${route}.\n${error.message}&#x60;, this, {error});
} else {
this.loggingService.sendErrorLevelMessage(&#x60;Nobody threw an error but something happened on route ${route}!&#x60;, this, {error});
}
}
/**
* Checks if an error is of type warning.
*
* @param errorTraceString
* @param errorTraceString - A description of the error and it&#x27;s stack trace.
* @returns true - If the error is of type warning.
* @private
*/
private isWarning(errorTraceString: string): boolean {
@@ -521,6 +603,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 &#x3D; this.router.url;
if (error instanceof HttpErrorResponse) {
this.loggingService.sendErrorLevelMessage(
&#x60;There was an HTTP error on route ${route}.\n${error.message}.\nStatus code: ${(error as HttpErrorResponse).status}&#x60;,
this, {error});
} else if (error instanceof TypeError) {
this.loggingService.sendErrorLevelMessage(&#x60;There was a Type error on route ${route}.\n${error.message}&#x60;, this, {error});
} else if (error instanceof Error) {
this.loggingService.sendErrorLevelMessage(&#x60;There was a general error on route ${route}.\n${error.message}&#x60;, this, {error});
} else {
this.loggingService.sendErrorLevelMessage(&#x60;Nobody threw an error but something happened on route ${route}!&#x60;, this, {error});
}
}
}
</code></pre>
</div>