Skip to content
This repository was archived by the owner on Aug 28, 2024. It is now read-only.

Commit 4b90cca

Browse files
simplify extensions
1 parent 6c850ff commit 4b90cca

3 files changed

Lines changed: 66 additions & 81 deletions

File tree

src/utils/exceptions.ts

Lines changed: 58 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,151 +1,130 @@
1-
import { Logger, LogLevel, LogEntry } from "../utils/logging";
1+
import { Logger, LogLevel } from "../utils/logging";
22

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}` });
255
}
266

277
/**
288
* Represents an exception with an HttpClient request
299
*
3010
*/
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 {
3712

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 }));
4017
}
4118
}
4219

43-
export class NoCacheAvailableException extends Exception {
20+
export class NoCacheAvailableException extends Error {
4421

4522
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);
4826
}
4927
}
5028

51-
export class APIUrlException extends Exception {
29+
export class APIUrlException extends Error {
5230

5331
constructor(msg = "Unable to determine API url.") {
54-
super("APIUrlException", msg);
55-
super.Log();
32+
super(msg);
33+
this.name = "APIUrlException";
34+
defaultLog(this);
5635
}
5736
}
5837

59-
export class AuthUrlException extends Exception {
38+
export class AuthUrlException extends Error {
6039

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 });
6944
}
7045
}
7146

72-
export class NodeFetchClientUnsupportedException extends Exception {
47+
export class NodeFetchClientUnsupportedException extends Error {
7348

7449
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);
7753
}
7854
}
7955

80-
export class SPRequestExecutorUndefinedException extends Exception {
56+
export class SPRequestExecutorUndefinedException extends Error {
8157

8258
constructor() {
8359
let msg = [
8460
"SP.RequestExecutor is undefined. ",
8561
"Load the SP.RequestExecutor.js library (/_layouts/15/SP.RequestExecutor.js) before loading the PnP JS Core library.",
8662
].join(" ");
87-
super("SPRequestExecutorUndefinedException", msg);
88-
super.Log();
63+
super(msg);
64+
this.name = "SPRequestExecutorUndefinedException";
65+
defaultLog(this);
8966
}
9067
}
9168

92-
export class MaxCommentLengthException extends Exception {
69+
export class MaxCommentLengthException extends Error {
9370

9471
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);
9775
}
9876
}
9977

100-
export class NotSupportedInBatchException extends Exception {
78+
export class NotSupportedInBatchException extends Error {
10179

10280
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);
10584
}
10685
}
10786

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 {
11488

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 });
11893
}
11994
}
12095

121-
export class BatchParseException extends Exception {
96+
export class BatchParseException extends Error {
12297

12398
constructor(msg: string) {
124-
super("BatchParseException", msg);
125-
super.Log();
99+
super(msg);
100+
this.name = "BatchParseException";
101+
defaultLog(this);
126102
}
127103
}
128104

129-
export class AlreadyInBatchException extends Exception {
105+
export class AlreadyInBatchException extends Error {
130106

131107
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);
134111
}
135112
}
136113

137-
export class FunctionExpectedException extends Exception {
114+
export class FunctionExpectedException extends Error {
138115

139116
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);
142120
}
143121
}
144122

145-
export class UrlException extends Exception {
123+
export class UrlException extends Error {
146124

147125
constructor(msg: string) {
148-
super("UrlException", msg);
149-
super.Log();
126+
super(msg);
127+
this.name = "UrlException";
128+
defaultLog(this);
150129
}
151130
}

tsconfig.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,8 @@
1818
"noUnusedLocals": true,
1919
"noUnusedParameters": true,
2020
"pretty": true
21-
}
21+
},
22+
"include": [
23+
"src/**/*"
24+
]
2225
}

webpack-serve.config.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@ module.exports = {
2222
],
2323
module: {
2424
loaders: [
25-
{ test: /\.ts$/, loader: 'babel-loader?presets[]=es2015!ts-loader' },
25+
{
26+
test: /\.ts$/,
27+
loader: 'babel-loader?presets[]=es2015!ts-loader'
28+
}
2629
]
2730
}
2831
};

0 commit comments

Comments
 (0)