Skip to content

Commit 02d3509

Browse files
committed
fix: Introduce proper error for invalid URL
Signed-off-by: Marcel Klehr <[email protected]>
1 parent fad29c1 commit 02d3509

File tree

6 files changed

+36
-6
lines changed

6 files changed

+36
-6
lines changed

_locales/en/messages.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,9 @@
134134
"Error045": {
135135
"message": "E045: Unexpected folder path. The local sync folder for this profile used to be at `{0}` but is now at `{1}`. Please make sure this is intended and set the local sync folder again in the profile settings."
136136
},
137+
"Error046": {
138+
"message": "E046: Invalid URL. `{0}` is not a valid URL."
139+
},
137140
"LabelWebdavurl": {
138141
"message": "WebDAV URL"
139142
},

src/errors/Error.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -392,4 +392,15 @@ export class UnexpectedFolderPathError extends FloccusError {
392392
this.newPath = newPath
393393
Object.setPrototypeOf(this, UnexpectedFolderPathError.prototype)
394394
}
395+
}
396+
397+
export class InvalidUrlError extends FloccusError {
398+
public url: string
399+
400+
constructor(url: string) {
401+
super(`E046: Invalid URL. '${url}' is not a valid URL.`)
402+
this.code = 46
403+
this.url = url
404+
Object.setPrototypeOf(this, InvalidUrlError.prototype)
405+
}
395406
}

src/lib/adapters/NextcloudBookmarks.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import {
2828
UnknownCreateTargetError,
2929
UnknownFolderParentUpdateError,
3030
UnknownFolderUpdateError,
31-
UnknownMoveTargetError, UpdateBookmarkError
31+
UnknownMoveTargetError, UpdateBookmarkError, InvalidUrlError
3232
} from '../../errors/Error'
3333

3434
const PAGE_SIZE = 300
@@ -122,7 +122,12 @@ export default class NextcloudBookmarksAdapter implements Adapter, BulkImportRes
122122
}
123123

124124
normalizeServerURL(input:string):string {
125-
const serverURL = new URL(input)
125+
let serverURL
126+
try {
127+
serverURL = new URL(input)
128+
} catch (e) {
129+
throw new InvalidUrlError(input)
130+
}
126131
const indexLoc = serverURL.pathname.indexOf('index.php')
127132
if (!serverURL.pathname) serverURL.pathname = ''
128133
serverURL.search = ''

src/lib/adapters/WebDav.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
HttpError, CancelledSyncError,
1111
LockFileError, MissingPermissionsError,
1212
NetworkError, RedirectError, ResourceLockedError,
13-
SlashError, FileSizeMismatch, FileSizeUnknown
13+
SlashError, FileSizeMismatch, FileSizeUnknown, InvalidUrlError
1414
} from '../../errors/Error'
1515
import { CapacitorHttp as Http } from '@capacitor/core'
1616
import { Capacitor } from '@capacitor/core'
@@ -56,7 +56,12 @@ export default class WebDavAdapter extends CachingAdapter {
5656
}
5757

5858
normalizeServerURL(input) {
59-
const serverURL = new URL(input)
59+
let serverURL
60+
try {
61+
serverURL = new URL(input)
62+
} catch (e) {
63+
throw new InvalidUrlError(input)
64+
}
6065
if (!serverURL.pathname) serverURL.pathname = ''
6166
serverURL.search = ''
6267
serverURL.hash = ''

src/lib/browser/BrowserAccount.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
CreateBookmarkError,
99
DeletionFailsafeError, FloccusError, GitPushError,
1010
HttpError,
11-
InconsistentBookmarksExistenceError, LockFileError,
11+
InconsistentBookmarksExistenceError, InvalidUrlError, LockFileError,
1212
MissingItemOrderError,
1313
ParseResponseError, UnexpectedFolderPathError,
1414
UnknownFolderItemOrderError, UpdateBookmarkError
@@ -126,6 +126,9 @@ export default class BrowserAccount extends Account {
126126
if (er instanceof UnexpectedFolderPathError) {
127127
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.originalPath, er.newPath])
128128
}
129+
if (er instanceof InvalidUrlError) {
130+
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.url])
131+
}
129132
if (er instanceof FloccusError) {
130133
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'))
131134
}

src/lib/native/NativeAccount.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import {
88
CreateBookmarkError,
99
DeletionFailsafeError, FloccusError, GitPushError,
1010
HttpError,
11-
InconsistentBookmarksExistenceError, LockFileError,
11+
InconsistentBookmarksExistenceError, InvalidUrlError, LockFileError,
1212
MissingItemOrderError,
1313
ParseResponseError, UnexpectedFolderPathError,
1414
UnknownFolderItemOrderError, UpdateBookmarkError
@@ -103,6 +103,9 @@ export default class NativeAccount extends Account {
103103
if (er instanceof UnexpectedFolderPathError) {
104104
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.originalPath, er.newPath])
105105
}
106+
if (er instanceof InvalidUrlError) {
107+
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'), [er.url])
108+
}
106109
if (er instanceof FloccusError) {
107110
return i18n.getMessage('Error' + String(er.code).padStart(3, '0'))
108111
}

0 commit comments

Comments
 (0)