Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
simplify extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-rodgers committed Dec 30, 2016
1 parent 6c850ff commit 4b90cca
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 81 deletions.
137 changes: 58 additions & 79 deletions src/utils/exceptions.ts
Original file line number Diff line number Diff line change
@@ -1,151 +1,130 @@
import { Logger, LogLevel, LogEntry } from "../utils/logging";
import { Logger, LogLevel } from "../utils/logging";

/**
* Base class for creating exceptions
*
*/
export abstract class Exception extends Error {
/**
* @param name The Name given to the Error instance
* @param message The message contained by the Error instance
*
*/
constructor(name: string, message: string) {
super(message);
this.name = name;
}

protected getLogEntry(): Promise<LogEntry> {
return Promise.resolve(<LogEntry>{ data: {}, level: LogLevel.Error, message: this.message });
}

protected Log(): Promise<void> {
return this.getLogEntry().then(e => Logger.log(e));
}
function defaultLog(error: Error) {
Logger.log({ data: {}, level: LogLevel.Error, message: `[${error.name}]::${error.message}` });
}

/**
* Represents an exception with an HttpClient request
*
*/
export class ProcessHttpClientResponseException extends Exception {

constructor(private response: Response) {
super("ProcessHttpClientResponseException", `Error making HttpClient request in queryable: [${response.status}] ${response.statusText}`);
super.Log();
}
export class ProcessHttpClientResponseException extends Error {

protected getLogEntry(): Promise<LogEntry> {
return this.response.json().then(json => <LogEntry>{ data: json, level: LogLevel.Error, message: this.message });
constructor(response: Response) {
super(`Error making HttpClient request in queryable: [${response.status}] ${response.statusText}`);
this.name = "ProcessHttpClientResponseException";
response.json().then(json => Logger.log({ data: json, level: LogLevel.Error, message: this.message }));
}
}

export class NoCacheAvailableException extends Exception {
export class NoCacheAvailableException extends Error {

constructor(msg = "Cannot create a caching configuration provider since cache is not available.") {
super("NoCacheAvailableException", msg);
super.Log();
super(msg);
this.name = "NoCacheAvailableException";
defaultLog(this);
}
}

export class APIUrlException extends Exception {
export class APIUrlException extends Error {

constructor(msg = "Unable to determine API url.") {
super("APIUrlException", msg);
super.Log();
super(msg);
this.name = "APIUrlException";
defaultLog(this);
}
}

export class AuthUrlException extends Exception {
export class AuthUrlException extends Error {

constructor(protected data: any, msg = "Auth URL Endpoint could not be determined from data. Data logged.") {
super("APIUrlException", msg);
super.Log();
}

protected getLogEntry(): Promise<LogEntry> {
// if any loggers are listening give them the full details
return Promise.resolve(<LogEntry>{ data: this.data, level: LogLevel.Error, message: this.message });
constructor(data: any, msg = "Auth URL Endpoint could not be determined from data. Data logged.") {
super(msg);
this.name = "APIUrlException";
Logger.log({ data: data, level: LogLevel.Error, message: this.message });
}
}

export class NodeFetchClientUnsupportedException extends Exception {
export class NodeFetchClientUnsupportedException extends Error {

constructor(msg = "Using NodeFetchClient in the browser is not supported.") {
super("NodeFetchClientUnsupportedException", msg);
this.Log();
super(msg);
this.name = "NodeFetchClientUnsupportedException";
defaultLog(this);
}
}

export class SPRequestExecutorUndefinedException extends Exception {
export class SPRequestExecutorUndefinedException extends Error {

constructor() {
let msg = [
"SP.RequestExecutor is undefined. ",
"Load the SP.RequestExecutor.js library (/_layouts/15/SP.RequestExecutor.js) before loading the PnP JS Core library.",
].join(" ");
super("SPRequestExecutorUndefinedException", msg);
super.Log();
super(msg);
this.name = "SPRequestExecutorUndefinedException";
defaultLog(this);
}
}

export class MaxCommentLengthException extends Exception {
export class MaxCommentLengthException extends Error {

constructor(msg = "The maximum comment length is 1023 characters.") {
super("MaxCommentLengthException", msg);
this.Log();
super(msg);
this.name = "MaxCommentLengthException";
defaultLog(this);
}
}

export class NotSupportedInBatchException extends Exception {
export class NotSupportedInBatchException extends Error {

constructor(operation = "This operation") {
super("NotSupportedInBatchException", `${operation} is not supported as part of a batch.`);
super.Log();
super(`${operation} is not supported as part of a batch.`);
this.name = "NotSupportedInBatchException";
defaultLog(this);
}
}

export class ODataIdException extends Exception {

constructor(protected data: any, msg = "Could not extract odata id in object, you may be using nometadata. Object data logged to logger.") {
super("ODataIdException", msg);
super.Log();
}
export class ODataIdException extends Error {

protected getLogEntry(): Promise<LogEntry> {
// if any loggers are listening give them the full details
return Promise.resolve(<LogEntry>{ data: this.data, level: LogLevel.Error, message: this.message });
constructor(data: any, msg = "Could not extract odata id in object, you may be using nometadata. Object data logged to logger.") {
super(msg);
this.name = "ODataIdException";
Logger.log({ data: data, level: LogLevel.Error, message: this.message });
}
}

export class BatchParseException extends Exception {
export class BatchParseException extends Error {

constructor(msg: string) {
super("BatchParseException", msg);
super.Log();
super(msg);
this.name = "BatchParseException";
defaultLog(this);
}
}

export class AlreadyInBatchException extends Exception {
export class AlreadyInBatchException extends Error {

constructor(msg = "This query is already part of a batch.") {
super("AlreadyInBatchException", msg);
super.Log();
super(msg);
this.name = "AlreadyInBatchException";
defaultLog(this);
}
}

export class FunctionExpectedException extends Exception {
export class FunctionExpectedException extends Error {

constructor(msg = "This query is already part of a batch.") {
super("FunctionExpectedException", msg);
super.Log();
super(msg);
this.name = "FunctionExpectedException";
defaultLog(this);
}
}

export class UrlException extends Exception {
export class UrlException extends Error {

constructor(msg: string) {
super("UrlException", msg);
super.Log();
super(msg);
this.name = "UrlException";
defaultLog(this);
}
}
5 changes: 4 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@
"noUnusedLocals": true,
"noUnusedParameters": true,
"pretty": true
}
},
"include": [
"src/**/*"
]
}
5 changes: 4 additions & 1 deletion webpack-serve.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ module.exports = {
],
module: {
loaders: [
{ test: /\.ts$/, loader: 'babel-loader?presets[]=es2015!ts-loader' },
{
test: /\.ts$/,
loader: 'babel-loader?presets[]=es2015!ts-loader'
}
]
}
};

0 comments on commit 4b90cca

Please sign in to comment.