export class UrlEncodingError extends Error {
  originalError;

  constructor(originalError: Error) {
    super();
    this.originalError = originalError;
  }
}

export function validateEncodedUrl(url: URL) {
  // if we're able to decode all parts of the url pathname & query params, it means that the url is encoded correctly
  try {
    url.pathname.split('/').forEach(decodeURIComponent);
    Array.from(url.searchParams.keys()).forEach(decodeURIComponent);
    Array.from(url.searchParams.values()).forEach(decodeURIComponent);
  } catch (e) {
    if (e instanceof URIError) {
      throw new UrlEncodingError(e);
    }
    throw e;
  }
}
