-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore(client): use application errors instead of PlatformErrors #2337
Changes from 2 commits
fa5dca4
ac9d754
726dd2e
8cfdfd4
7c0fb81
530eceb
bc433ed
edac0c2
9457111
d27c5b1
465e6b1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,12 @@ | |
|
||
import {CustomError} from '@outline/infrastructure/custom_error'; | ||
|
||
import * as errors from './errors'; | ||
|
||
// TODO(fortuna): Remove PlatformError from the model. | ||
// PlatformError is an implementation detail. It does not belong in the model. | ||
// It's also about serialization, we should not use it in application code. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 As we have a unified |
||
|
||
/** | ||
* @fileoverview This file defines types and constants corresponding to the backend Go's | ||
* `platerrors` package. It will be used to receive native errors from Go, eventually replacing | ||
|
@@ -80,35 +86,6 @@ function convertRawErrorObjectToPlatformError(rawObj: object): PlatformError { | |
return new PlatformError(code, rawObj.message, options); | ||
} | ||
|
||
/** | ||
* Recursively converts a {@link PlatformError} into a raw JavaScript object that | ||
* could be converted into a JSON string. | ||
* @param {PlatformError} platErr Any non-null PlatformError. | ||
* @returns {object} A plain JavaScript object that can be converted to JSON. | ||
*/ | ||
function convertPlatformErrorToRawErrorObject(platErr: PlatformError): object { | ||
const rawObj: { | ||
code: string; | ||
message: string; | ||
details?: ErrorDetails; | ||
cause?: object; | ||
} = { | ||
code: platErr.code, | ||
message: platErr.message, | ||
details: platErr.details, | ||
}; | ||
if (platErr.cause) { | ||
let cause: PlatformError; | ||
if (platErr.cause instanceof PlatformError) { | ||
cause = platErr.cause; | ||
} else { | ||
cause = new PlatformError(INTERNAL_ERROR, String(platErr.cause)); | ||
} | ||
rawObj.cause = convertPlatformErrorToRawErrorObject(cause); | ||
} | ||
return rawObj; | ||
} | ||
|
||
/** | ||
* ErrorDetails represents the details map of a {@link PlatformError}. | ||
* The keys in this map are strings, and the values can be of any data type. | ||
|
@@ -216,14 +193,31 @@ export class PlatformError extends CustomError { | |
} | ||
return result; | ||
} | ||
} | ||
|
||
/** | ||
* Returns a JSON string of this error with all details and causes. | ||
* @returns {string} The JSON string representing this error. | ||
*/ | ||
toJSON(): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is used when initiating a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed. Ugh... Perhaps we can move the error serialization to Go at some point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, we should move the logic to Go in the future. All errors should be triggered from Go. |
||
const errRawObj = convertPlatformErrorToRawErrorObject(this); | ||
return JSON.stringify(errRawObj); | ||
/** | ||
* ipcToAppError converts an Error returned from the MethodChannel IPC to an application error. | ||
* MethodChannel errors encode its information as a JSON object in the Error message. | ||
*/ | ||
export function ipcToAppError(ipcError: Error): CustomError { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about using "parse" here, since the input is an object, not serialized text.
jyyi1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const platError = convertRawErrorObjectToPlatformError( | ||
JSON.parse(ipcError.message) | ||
); | ||
let options = undefined as {cause?: Error}; | ||
jyyi1 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (platError.cause instanceof Error) { | ||
options = {cause: platError.cause}; | ||
} | ||
switch (platError.code) { | ||
case FETCH_CONFIG_FAILED: | ||
return new errors.SessionConfigFetchFailed(platError.message, options); | ||
case ILLEGAL_CONFIG: | ||
return new errors.ServerAccessKeyInvalid(platError.message, options); | ||
case PROXY_SERVER_UNREACHABLE: | ||
return new errors.ServerUnreachable(platError.message, options); | ||
case VPN_PERMISSION_NOT_GRANTED: | ||
return new errors.VpnPermissionNotGranted(platError.message, options); | ||
default: | ||
return platError; | ||
} | ||
} | ||
|
||
|
@@ -240,13 +234,12 @@ export type ErrorCode = string; | |
|
||
export const INTERNAL_ERROR: ErrorCode = 'ERR_INTERNAL_ERROR'; | ||
|
||
export const FETCH_CONFIG_FAILED: ErrorCode = 'ERR_FETCH_CONFIG_FAILURE'; | ||
export const ILLEGAL_CONFIG: ErrorCode = 'ERR_ILLEGAL_CONFIG'; | ||
const FETCH_CONFIG_FAILED: ErrorCode = 'ERR_FETCH_CONFIG_FAILURE'; | ||
daniellacosse marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const ILLEGAL_CONFIG: ErrorCode = 'ERR_ILLEGAL_CONFIG'; | ||
|
||
export const VPN_PERMISSION_NOT_GRANTED = 'ERR_VPN_PERMISSION_NOT_GRANTED'; | ||
const VPN_PERMISSION_NOT_GRANTED = 'ERR_VPN_PERMISSION_NOT_GRANTED'; | ||
|
||
export const PROXY_SERVER_UNREACHABLE: ErrorCode = | ||
'ERR_PROXY_SERVER_UNREACHABLE'; | ||
const PROXY_SERVER_UNREACHABLE: ErrorCode = 'ERR_PROXY_SERVER_UNREACHABLE'; | ||
|
||
/** Indicates that the OS routing service is not running (electron only). */ | ||
export const ROUTING_SERVICE_NOT_RUNNING = 'ERR_ROUTING_SERVICE_NOT_RUNNING'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would keep this. The default handler (
showErrorCauseDialog
) will print weird outputs (for example, duplicated causes).PlatformError
'stoString
already handles the format and prints a cause chain.Also the default handler doesn't print out error
details
Map
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in
showErrorCauseDialog
.We shouldn't be using
localizeErrorCode
anymore, it's not needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where is localization of errors happening now? Are we no longer localizing these errors and just throwing a generic localized "unknown error" for them? That's a regression and is a worse user experience. We should localize the top-level user-facing error that can tell the user what went wrong (like the one suggesting to contact their service provider with details).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We localize the errors in
App.showLocalizedError
:outline-apps/client/src/www/app/app.ts
Line 246 in 2e3bd3d
I'm actually removing the duplicate logic in this PR.