|
1 | | -import { Logger, LogLevel, LogEntry } from "../utils/logging"; |
| 1 | +import { Logger, LogLevel } from "../utils/logging"; |
2 | 2 |
|
3 | | -/** |
4 | | - * Base class for creating exceptions |
5 | | - * |
6 | | - */ |
7 | | -export abstract class Exception extends Error { |
8 | | - /** |
9 | | - * @param name The Name given to the Error instance |
10 | | - * @param message The message contained by the Error instance |
11 | | - * |
12 | | - */ |
13 | | - constructor(name: string, message: string) { |
14 | | - super(message); |
15 | | - this.name = name; |
16 | | - } |
17 | | - |
18 | | - protected getLogEntry(): Promise<LogEntry> { |
19 | | - return Promise.resolve(<LogEntry>{ data: {}, level: LogLevel.Error, message: this.message }); |
20 | | - } |
21 | | - |
22 | | - protected Log(): Promise<void> { |
23 | | - return this.getLogEntry().then(e => Logger.log(e)); |
24 | | - } |
| 3 | +function defaultLog(error: Error) { |
| 4 | + Logger.log({ data: {}, level: LogLevel.Error, message: `[${error.name}]::${error.message}` }); |
25 | 5 | } |
26 | 6 |
|
27 | 7 | /** |
28 | 8 | * Represents an exception with an HttpClient request |
29 | 9 | * |
30 | 10 | */ |
31 | | -export class ProcessHttpClientResponseException extends Exception { |
32 | | - |
33 | | - constructor(private response: Response) { |
34 | | - super("ProcessHttpClientResponseException", `Error making HttpClient request in queryable: [${response.status}] ${response.statusText}`); |
35 | | - super.Log(); |
36 | | - } |
| 11 | +export class ProcessHttpClientResponseException extends Error { |
37 | 12 |
|
38 | | - protected getLogEntry(): Promise<LogEntry> { |
39 | | - return this.response.json().then(json => <LogEntry>{ data: json, level: LogLevel.Error, message: this.message }); |
| 13 | + constructor(response: Response) { |
| 14 | + super(`Error making HttpClient request in queryable: [${response.status}] ${response.statusText}`); |
| 15 | + this.name = "ProcessHttpClientResponseException"; |
| 16 | + response.json().then(json => Logger.log({ data: json, level: LogLevel.Error, message: this.message })); |
40 | 17 | } |
41 | 18 | } |
42 | 19 |
|
43 | | -export class NoCacheAvailableException extends Exception { |
| 20 | +export class NoCacheAvailableException extends Error { |
44 | 21 |
|
45 | 22 | constructor(msg = "Cannot create a caching configuration provider since cache is not available.") { |
46 | | - super("NoCacheAvailableException", msg); |
47 | | - super.Log(); |
| 23 | + super(msg); |
| 24 | + this.name = "NoCacheAvailableException"; |
| 25 | + defaultLog(this); |
48 | 26 | } |
49 | 27 | } |
50 | 28 |
|
51 | | -export class APIUrlException extends Exception { |
| 29 | +export class APIUrlException extends Error { |
52 | 30 |
|
53 | 31 | constructor(msg = "Unable to determine API url.") { |
54 | | - super("APIUrlException", msg); |
55 | | - super.Log(); |
| 32 | + super(msg); |
| 33 | + this.name = "APIUrlException"; |
| 34 | + defaultLog(this); |
56 | 35 | } |
57 | 36 | } |
58 | 37 |
|
59 | | -export class AuthUrlException extends Exception { |
| 38 | +export class AuthUrlException extends Error { |
60 | 39 |
|
61 | | - constructor(protected data: any, msg = "Auth URL Endpoint could not be determined from data. Data logged.") { |
62 | | - super("APIUrlException", msg); |
63 | | - super.Log(); |
64 | | - } |
65 | | - |
66 | | - protected getLogEntry(): Promise<LogEntry> { |
67 | | - // if any loggers are listening give them the full details |
68 | | - return Promise.resolve(<LogEntry>{ data: this.data, level: LogLevel.Error, message: this.message }); |
| 40 | + constructor(data: any, msg = "Auth URL Endpoint could not be determined from data. Data logged.") { |
| 41 | + super(msg); |
| 42 | + this.name = "APIUrlException"; |
| 43 | + Logger.log({ data: data, level: LogLevel.Error, message: this.message }); |
69 | 44 | } |
70 | 45 | } |
71 | 46 |
|
72 | | -export class NodeFetchClientUnsupportedException extends Exception { |
| 47 | +export class NodeFetchClientUnsupportedException extends Error { |
73 | 48 |
|
74 | 49 | constructor(msg = "Using NodeFetchClient in the browser is not supported.") { |
75 | | - super("NodeFetchClientUnsupportedException", msg); |
76 | | - this.Log(); |
| 50 | + super(msg); |
| 51 | + this.name = "NodeFetchClientUnsupportedException"; |
| 52 | + defaultLog(this); |
77 | 53 | } |
78 | 54 | } |
79 | 55 |
|
80 | | -export class SPRequestExecutorUndefinedException extends Exception { |
| 56 | +export class SPRequestExecutorUndefinedException extends Error { |
81 | 57 |
|
82 | 58 | constructor() { |
83 | 59 | let msg = [ |
84 | 60 | "SP.RequestExecutor is undefined. ", |
85 | 61 | "Load the SP.RequestExecutor.js library (/_layouts/15/SP.RequestExecutor.js) before loading the PnP JS Core library.", |
86 | 62 | ].join(" "); |
87 | | - super("SPRequestExecutorUndefinedException", msg); |
88 | | - super.Log(); |
| 63 | + super(msg); |
| 64 | + this.name = "SPRequestExecutorUndefinedException"; |
| 65 | + defaultLog(this); |
89 | 66 | } |
90 | 67 | } |
91 | 68 |
|
92 | | -export class MaxCommentLengthException extends Exception { |
| 69 | +export class MaxCommentLengthException extends Error { |
93 | 70 |
|
94 | 71 | constructor(msg = "The maximum comment length is 1023 characters.") { |
95 | | - super("MaxCommentLengthException", msg); |
96 | | - this.Log(); |
| 72 | + super(msg); |
| 73 | + this.name = "MaxCommentLengthException"; |
| 74 | + defaultLog(this); |
97 | 75 | } |
98 | 76 | } |
99 | 77 |
|
100 | | -export class NotSupportedInBatchException extends Exception { |
| 78 | +export class NotSupportedInBatchException extends Error { |
101 | 79 |
|
102 | 80 | constructor(operation = "This operation") { |
103 | | - super("NotSupportedInBatchException", `${operation} is not supported as part of a batch.`); |
104 | | - super.Log(); |
| 81 | + super(`${operation} is not supported as part of a batch.`); |
| 82 | + this.name = "NotSupportedInBatchException"; |
| 83 | + defaultLog(this); |
105 | 84 | } |
106 | 85 | } |
107 | 86 |
|
108 | | -export class ODataIdException extends Exception { |
109 | | - |
110 | | - constructor(protected data: any, msg = "Could not extract odata id in object, you may be using nometadata. Object data logged to logger.") { |
111 | | - super("ODataIdException", msg); |
112 | | - super.Log(); |
113 | | - } |
| 87 | +export class ODataIdException extends Error { |
114 | 88 |
|
115 | | - protected getLogEntry(): Promise<LogEntry> { |
116 | | - // if any loggers are listening give them the full details |
117 | | - return Promise.resolve(<LogEntry>{ data: this.data, level: LogLevel.Error, message: this.message }); |
| 89 | + constructor(data: any, msg = "Could not extract odata id in object, you may be using nometadata. Object data logged to logger.") { |
| 90 | + super(msg); |
| 91 | + this.name = "ODataIdException"; |
| 92 | + Logger.log({ data: data, level: LogLevel.Error, message: this.message }); |
118 | 93 | } |
119 | 94 | } |
120 | 95 |
|
121 | | -export class BatchParseException extends Exception { |
| 96 | +export class BatchParseException extends Error { |
122 | 97 |
|
123 | 98 | constructor(msg: string) { |
124 | | - super("BatchParseException", msg); |
125 | | - super.Log(); |
| 99 | + super(msg); |
| 100 | + this.name = "BatchParseException"; |
| 101 | + defaultLog(this); |
126 | 102 | } |
127 | 103 | } |
128 | 104 |
|
129 | | -export class AlreadyInBatchException extends Exception { |
| 105 | +export class AlreadyInBatchException extends Error { |
130 | 106 |
|
131 | 107 | constructor(msg = "This query is already part of a batch.") { |
132 | | - super("AlreadyInBatchException", msg); |
133 | | - super.Log(); |
| 108 | + super(msg); |
| 109 | + this.name = "AlreadyInBatchException"; |
| 110 | + defaultLog(this); |
134 | 111 | } |
135 | 112 | } |
136 | 113 |
|
137 | | -export class FunctionExpectedException extends Exception { |
| 114 | +export class FunctionExpectedException extends Error { |
138 | 115 |
|
139 | 116 | constructor(msg = "This query is already part of a batch.") { |
140 | | - super("FunctionExpectedException", msg); |
141 | | - super.Log(); |
| 117 | + super(msg); |
| 118 | + this.name = "FunctionExpectedException"; |
| 119 | + defaultLog(this); |
142 | 120 | } |
143 | 121 | } |
144 | 122 |
|
145 | | -export class UrlException extends Exception { |
| 123 | +export class UrlException extends Error { |
146 | 124 |
|
147 | 125 | constructor(msg: string) { |
148 | | - super("UrlException", msg); |
149 | | - super.Log(); |
| 126 | + super(msg); |
| 127 | + this.name = "UrlException"; |
| 128 | + defaultLog(this); |
150 | 129 | } |
151 | 130 | } |
0 commit comments