Skip to content

Commit

Permalink
feat(client): add details to custom error (#1977)
Browse files Browse the repository at this point in the history
* feat(client): add cause to custom error

* Update client/src/www/app/outline_server_repository/access_key_serialization.ts

Co-authored-by: Vinicius Fortuna <[email protected]>

* clarify cause and details

* fix sloppyness

* Update client/src/www/model/errors.ts

Co-authored-by: Sander Bruens <[email protected]>

* not private

* fix issue

---------

Co-authored-by: Vinicius Fortuna <[email protected]>
Co-authored-by: Sander Bruens <[email protected]>
  • Loading branch information
3 people authored May 3, 2024
1 parent 27f579f commit 3e2aedf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
25 changes: 19 additions & 6 deletions client/src/www/app/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -204,30 +204,38 @@ export class App {
toastMessage = this.localize('error-connection-configuration');
buttonMessage = this.localize('error-details');
buttonHandler = () => {
this.showErrorDetailDialog(error);
this.showErrorCauseDialog(error);
};
} else if (error instanceof errors.SessionConfigFetchFailed) {
toastMessage = this.localize('error-connection-configuration-fetch');
buttonMessage = this.localize('error-details');
buttonHandler = () => {
this.showErrorDetailDialog(error);
this.showErrorCauseDialog(error);
};
} else if (error instanceof errors.ProxyConnectionFailure) {
toastMessage = this.localize('error-connection-proxy');
buttonMessage = this.localize('error-details');
buttonHandler = () => {
this.showErrorDetailDialog(error);
this.showErrorCauseDialog(error);
};
} else if (error instanceof errors.SessionConfigError) {
toastMessage = error.message;
} else if (error instanceof errors.SessionProviderError) {
toastMessage = error.message;
buttonMessage = this.localize('error-details');

console.log(error, error.message, error.details);
buttonHandler = () => {
this.showErrorDetailsDialog(error.details);
};
} else {
const hasErrorDetails = Boolean(error.message || error.cause);
toastMessage = this.localize('error-unexpected');

if (hasErrorDetails) {
buttonMessage = this.localize('error-details');
buttonHandler = () => {
this.showErrorDetailDialog(error);
this.showErrorCauseDialog(error);
};
}
}
Expand Down Expand Up @@ -573,18 +581,23 @@ export class App {
return new Promise<boolean>(resolve => resolve(confirm(message)));
}

private showErrorDetailDialog(error: Error) {
private showErrorCauseDialog(error: Error) {
let message = error.toString();

if (error.cause) {
message += '\nCause: ';
message += error.cause.toString();
}

// Temporarily use window.alert here
return alert(message);
}

private showErrorDetailsDialog(details: string) {
if (!details) return;

return alert(details);
}

//#endregion UI dialogs

// Helpers:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function parseShadowsocksSessionConfigJson(responseBody: string): ShadowsocksSes
const responseJson = JSON.parse(responseBody);

if ('error' in responseJson) {
throw new errors.SessionConfigError(responseJson.error.message);
throw new errors.SessionProviderError(responseJson.error.message, responseJson.error.details);
}

const {method, password, server, server_port, prefix} = responseJson;
Expand Down Expand Up @@ -85,7 +85,7 @@ export async function fetchShadowsocksSessionConfig(configLocation: URL): Promis

return parseShadowsocksSessionConfigJson(responseBody);
} catch (cause) {
if (cause instanceof errors.SessionConfigError) {
if (cause instanceof errors.SessionProviderError) {
throw cause;
}

Expand Down
12 changes: 11 additions & 1 deletion client/src/www/model/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,18 @@ export class SessionConfigFetchFailed extends CustomError {
}

export class SessionConfigError extends CustomError {
constructor(message: string) {
constructor(message: string, options?: {cause?: Error}) {
super(message, options);
}
}

export class SessionProviderError extends CustomError {
readonly details: string | undefined;

constructor(message: string, details?: string) {
super(message);

this.details = details;
}
}

Expand Down

0 comments on commit 3e2aedf

Please sign in to comment.