Skip to content

Commit

Permalink
fix: only append retry url if current location is available
Browse files Browse the repository at this point in the history
  • Loading branch information
pascaliske committed Feb 11, 2022
1 parent 784d9ff commit a71b77e
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/app/pages/error/valid-code.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,43 @@ export class ValidCodeGuard implements CanActivate {
*/
public canActivate(route: ActivatedRouteSnapshot): boolean | UrlTree {
// show empty error page if no code is present
if (route?.queryParamMap?.has('code') ?? false) {
if (!(route?.queryParamMap?.has('code') ?? false)) {
return true
}

// redirect to not found if no valid code is present
if (!/^\d{3}$/.test(route?.queryParamMap?.get('code') ?? '')) {
console.info(`No valid error code found, redirecting to ${StatusCodes.NOT_FOUND}.`)

// prepare retry url
const retry = new URL(this.document.location?.href ?? '')
retry.searchParams.delete('code')
retry.searchParams.delete('retry')

// redirect to 404 if not valid
return this.router.createUrlTree([this.document.location?.pathname ?? '/'], {
return this.router.createUrlTree([this.document?.location?.pathname ?? '/'], {
queryParams: {
code: StatusCodes.NOT_FOUND,
home: route?.queryParams.home,
retry: retry.toString(),
retry: this.buildRetryUrl(),
},
})
}

// show error page for code parameter
return true
}

/**
* Tries to build a retry URL from the current location href.
*
* @returns Returns the retry URL string or null.
*/
private buildRetryUrl(): string | null {
if (this.document?.location?.href?.length === 0) {
return null
}

// remove code and retry parameters to prevent infinite loop
const retry = new URL(this.document?.location?.href ?? '')
retry.searchParams.delete('code')
retry.searchParams.delete('retry')

return retry.toString()
}
}

0 comments on commit a71b77e

Please sign in to comment.