Skip to content

Commit

Permalink
feat(#117): validate server URL for more expressive error
Browse files Browse the repository at this point in the history
  • Loading branch information
lukashornych committed Sep 30, 2024
1 parent 7d964bc commit 5f66dbc
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions src/modules/connection/model/Connection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import XXH, { HashObject } from 'xxhashjs'
import { ConnectionId } from '@/modules/connection/model/ConnectionId'
import { UnexpectedError } from '@/modules/base/exception/UnexpectedError'

const hasher: HashObject = XXH.h64()

Expand Down Expand Up @@ -29,7 +30,7 @@ export class Connection {
this.id = id ? id : hasher.update(name).digest().toString(16)
this.name = name
this.preconfigured = preconfigured
this.serverUrl = this.normalizeUrl(serverUrl)
this.serverUrl = this.validateAndNormalizeUrl(serverUrl)
}

static user(id: ConnectionId | undefined,
Expand Down Expand Up @@ -58,7 +59,7 @@ export class Connection {
json.id,
json.name,
true,
json.serverurl
json.serverUrl
)
}

Expand Down Expand Up @@ -90,7 +91,12 @@ export class Connection {
return this._observabilityUrl
}

private normalizeUrl(url: string): string {
private validateAndNormalizeUrl(url: string): string {
try {
new URL(url)
} catch (e) {
throw new UnexpectedError('Server URL is not valid URL.')
}
return url.endsWith('/') ? url.substring(0, url.length - 1) : url
}
}

0 comments on commit 5f66dbc

Please sign in to comment.