Skip to content

Commit

Permalink
feat: #73 improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
pamapa committed Oct 26, 2021
1 parent 1d5f87a commit 382913f
Show file tree
Hide file tree
Showing 32 changed files with 441 additions and 335 deletions.
16 changes: 10 additions & 6 deletions src/AccessTokenEvents.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log, Timer } from "./utils";
import { Logger, Timer } from "./utils";
import type { User } from "./User";

export type AccessTokenCallback = (...ev: any[]) => void;
Expand All @@ -10,11 +10,15 @@ export type AccessTokenCallback = (...ev: any[]) => void;
* @public
*/
export class AccessTokenEvents {
protected _logger: Logger;

private _expiringNotificationTimeInSeconds: number
private _expiringTimer: Timer
private _expiredTimer: Timer

public constructor({ expiringNotificationTimeInSeconds }: { expiringNotificationTimeInSeconds: number }) {
this._logger = new Logger("AccessTokenEvents");

this._expiringNotificationTimeInSeconds = expiringNotificationTimeInSeconds;
this._expiringTimer = new Timer("Access token expiring");
this._expiredTimer = new Timer("Access token expired");
Expand All @@ -24,7 +28,7 @@ export class AccessTokenEvents {
// only register events if there's an access token and it has an expiration
if (container.access_token && container.expires_in !== undefined) {
const duration = container.expires_in;
Log.debug("AccessTokenEvents.load: access token present, remaining duration:", duration);
this._logger.debug("load: access token present, remaining duration:", duration);

if (duration > 0) {
// only register expiring if we still have time
Expand All @@ -33,17 +37,17 @@ export class AccessTokenEvents {
expiring = 1;
}

Log.debug("AccessTokenEvents.load: registering expiring timer in:", expiring);
this._logger.debug("load: registering expiring timer in:", expiring);
this._expiringTimer.init(expiring);
}
else {
Log.debug("AccessTokenEvents.load: canceling existing expiring timer becase we're past expiration.");
this._logger.debug("load: canceling existing expiring timer because we're past expiration.");
this._expiringTimer.cancel();
}

// if it's negative, it will still fire
const expired = duration + 1;
Log.debug("AccessTokenEvents.load: registering expired timer in:", expired);
this._logger.debug("load: registering expired timer in:", expired);
this._expiredTimer.init(expired);
}
else {
Expand All @@ -53,7 +57,7 @@ export class AccessTokenEvents {
}

public unload(): void {
Log.debug("AccessTokenEvents.unload: canceling existing access token timers");
this._logger.debug("unload: canceling existing access token timers");
this._expiringTimer.cancel();
this._expiredTimer.cancel();
}
Expand Down
15 changes: 9 additions & 6 deletions src/CheckSessionIFrame.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

/**
* @public
*/
export class CheckSessionIFrame {
protected readonly _logger: Logger;
private _frame_origin: string;
private _frame: HTMLIFrameElement;
private _timer: number | null = null
Expand All @@ -19,6 +20,8 @@ export class CheckSessionIFrame {
private _intervalInSeconds: number,
private _stopOnError: boolean
) {
this._logger = new Logger("CheckSessionIFrame");

const idx = url.indexOf("/", url.indexOf("//") + 2);
this._frame_origin = url.substr(0, idx);

Expand Down Expand Up @@ -50,18 +53,18 @@ export class CheckSessionIFrame {
e.source === this._frame.contentWindow
) {
if (e.data === "error") {
Log.error("CheckSessionIFrame: error message from check session op iframe");
this._logger.error("error message from check session op iframe");
if (this._stopOnError) {
this.stop();
}
}
else if (e.data === "changed") {
Log.debug("CheckSessionIFrame: changed message from check session op iframe");
this._logger.debug("changed message from check session op iframe");
this.stop();
void this._callback();
}
else {
Log.debug("CheckSessionIFrame: " + e.data + " message from check session op iframe");
this._logger.debug(e.data + " message from check session op iframe");
}
}
}
Expand All @@ -71,7 +74,7 @@ export class CheckSessionIFrame {
return;
}

Log.debug("CheckSessionIFrame.start");
this._logger.debug("start");

this.stop();

Expand All @@ -96,7 +99,7 @@ export class CheckSessionIFrame {
this._session_state = null;

if (this._timer) {
Log.debug("CheckSessionIFrame.stop");
this._logger.debug("stop");

window.clearInterval(this._timer);
this._timer = null;
Expand Down
10 changes: 5 additions & 5 deletions src/ErrorResponse.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

export class ErrorResponse extends Error {
public readonly name: string;
Expand All @@ -16,13 +16,13 @@ export class ErrorResponse extends Error {
public constructor(args: {
error?: string; error_description?: string; error_uri?: string; state?: any; session_state?: string;
}) {
super(args.error_description || args.error);

if (!args.error) {
Log.error("No error passed to ErrorResponse");
throw new Error("error");
Logger.error("ErrorResponse", "No error passed");
throw new Error("No error passed");
}

super(args.error_description || args.error);

this.name = "ErrorResponse";

this.error = args.error;
Expand Down
12 changes: 7 additions & 5 deletions src/InMemoryWebStorage.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,37 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

/**
* @public
*/
export class InMemoryWebStorage implements Storage {
private readonly _logger: Logger;
private _data: Record<string, string>;

public constructor() {
this._logger = new Logger("InMemoryWebStorage");
this._data = {};
}

public clear(): void {
Log.debug("InMemoryWebStorage.clear");
this._logger.debug("clear");
this._data = {};
}

public getItem(key: string): string {
Log.debug("InMemoryWebStorage.getItem", key);
this._logger.debug("getItem", key);
return this._data[key];
}

public setItem(key: string, value: string): void {
Log.debug("InMemoryWebStorage.setItem", key);
this._logger.debug("setItem", key);
this._data[key] = value;
}

public removeItem(key: string): void {
Log.debug("InMemoryWebStorage.removeItem", key);
this._logger.debug("removeItem", key);
delete this._data[key];
}

Expand Down
35 changes: 21 additions & 14 deletions src/JsonService.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
// Copyright (c) Brock Allen & Dominick Baier. All rights reserved.
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.

import { Log } from "./utils";
import { Logger } from "./utils";

export type JwtHandler = (text: string) => Promise<any>;

export class JsonService {
private readonly _logger: Logger;

private _contentTypes: string[];
private _jwtHandler: JwtHandler | null;

public constructor(
additionalContentTypes: string[] = [],
jwtHandler: JwtHandler | null = null
) {
this._logger = new Logger("JsonService");

this._contentTypes = additionalContentTypes.slice();
this._contentTypes.push("application/json");
if (jwtHandler) {
Expand All @@ -24,27 +28,27 @@ export class JsonService {

public async getJson(url: string, token?: string): Promise<any> {
if (!url) {
Log.error("JsonService.getJson: No url passed");
this._logger.error("getJson: No url passed");
throw new Error("url");
}

const headers: HeadersInit = {};
if (token) {
Log.debug("JsonService.getJson: token passed, setting Authorization header");
this._logger.debug("getJson: token passed, setting Authorization header");
headers["Authorization"] = "Bearer " + token;
}

let response: Response;
try {
Log.debug("JsonService.getJson, url: ", url);
this._logger.debug("getJson, url:", url);
response = await fetch(url, { method: "GET", headers });
}
catch (err) {
Log.error("JsonService.getJson: network error");
this._logger.error("getJson: network error");
throw new Error("Network Error");
}

Log.debug("JsonService.getJson: HTTP response received, status", response.status);
this._logger.debug("getJson: HTTP response received, status", response.status);
if (response.status === 200) {
const contentType = response.headers.get("Content-Type");
if (contentType) {
Expand All @@ -60,7 +64,8 @@ export class JsonService {
return json;
}
catch (err) {
Log.error("JsonService.getJson: Error parsing JSON response", err instanceof Error ? err.message : err);
const msg = err instanceof Error ? err.message : err;
this._logger.error("getJson: Error parsing JSON response", msg);
throw err;
}
}
Expand All @@ -74,7 +79,7 @@ export class JsonService {

public async postForm(url: string, payload: any, basicAuth?: string): Promise<any> {
if (!url) {
Log.error("JsonService.postForm: No url passed");
this._logger.error("postForm: No url passed");
throw new Error("url");
}

Expand All @@ -96,17 +101,17 @@ export class JsonService {

let response: Response;
try {
Log.debug("JsonService.postForm, url: ", url);
this._logger.debug("postForm, url:", url);
response = await fetch(url, { method: "POST", headers, body });
}
catch (err) {
Log.error("JsonService.postForm: network error");
this._logger.error("postForm: network error");
throw new Error("Network Error");
}

const allowedContentTypes = this._contentTypes;

Log.debug("JsonService.postForm: HTTP response received, status", response.status);
this._logger.debug("postForm: HTTP response received, status", response.status);
if (response.status === 200) {
const contentType = response.headers.get("Content-Type");
if (contentType) {
Expand All @@ -117,7 +122,8 @@ export class JsonService {
return json;
}
catch (err) {
Log.error("JsonService.postForm: Error parsing JSON response", err instanceof Error ? err.message : err);
const msg = err instanceof Error ? err.message : err;
this._logger.error("postForm: Error parsing JSON response", msg);
throw err;
}
}
Expand All @@ -133,14 +139,15 @@ export class JsonService {
try {
const json = await response.json();
if (json && json.error) {
Log.error("JsonService.postForm: Error from server: ", json.error);
this._logger.error("postForm: Error from server:", json.error);
throw new Error(payload.error);
}

return json;
}
catch (err) {
Log.error("JsonService.postForm: Error parsing JSON response", err instanceof Error ? err.message : err);
const msg = err instanceof Error ? err.message : err;
this._logger.error("postForm: Error parsing JSON response", msg);
throw err;
}
}
Expand Down
Loading

0 comments on commit 382913f

Please sign in to comment.