{"version":3,"file":"HttpErrorResult.mjs","names":[],"sources":["../../../../src/requester/results/error/HttpErrorResult.ts"],"sourcesContent":["import type { Resource } from \"@ldo/connected\";\nimport { ResourceError } from \"@ldo/connected\";\n\n/**\n * A set of standard errors that can be returned as a result of an HTTP request\n */\nexport type HttpErrorResultType<ResourceType extends Resource> =\n  | ServerHttpError<ResourceType>\n  | UnexpectedHttpError<ResourceType>\n  | UnauthenticatedHttpError<ResourceType>\n  | UnauthorizedHttpError<ResourceType>;\n\n/**\n * An error caused by an HTTP request\n */\nexport abstract class HttpErrorResult<\n  ResourceType extends Resource,\n> extends ResourceError<ResourceType> {\n  /**\n   * The status of the HTTP request\n   */\n  public readonly status: number;\n\n  /**\n   * Headers returned by the HTTP request\n   */\n  public readonly headers: Headers;\n\n  /**\n   * Response returned by the HTTP request\n   */\n  public readonly response: Response;\n\n  /**\n   * @param resource - the resource\n   * @param response - The response returned by the HTTP requests\n   * @param message - A custom message for the error\n   */\n  constructor(resource: ResourceType, response: Response, message?: string) {\n    super(\n      resource,\n      message ||\n        `Request for ${resource.uri} returned ${response.status} (${response.statusText}).`,\n    );\n    this.status = response.status;\n    this.headers = response.headers;\n    this.response = response;\n  }\n\n  /**\n   * Checks to see if a given response does not constitute an HTTP Error\n   * @param response - The response of the request\n   * @returns true if the response does not constitute an HTTP Error\n   */\n  static isnt(response: Response) {\n    return (\n      !(response.status >= 200 && response.status < 300) &&\n      response.status !== 404 &&\n      response.status !== 304\n    );\n  }\n\n  /**\n   * Checks a given response to see if it is a ServerHttpError, an\n   * UnauthenticatedHttpError or a some unexpected error.\n   * @param uri - The uri of the request\n   * @param response - The response of the request\n   * @returns An error if the response calls for it. Undefined if not.\n   */\n  static checkResponse<ResourceType extends Resource>(\n    resource: ResourceType,\n    response: Response,\n  ): HttpErrorResultType<ResourceType> | undefined {\n    if (ServerHttpError.is(response)) {\n      return new ServerHttpError(resource, response);\n    }\n    if (UnauthenticatedHttpError.is(response)) {\n      return new UnauthenticatedHttpError(resource, response);\n    }\n    if (UnauthorizedHttpError.is(response)) {\n      return new UnauthorizedHttpError(resource, response);\n    }\n    if (HttpErrorResult.isnt(response)) {\n      return new UnexpectedHttpError(resource, response);\n    }\n    return undefined;\n  }\n}\n\n/**\n * An unexpected error as a result of an HTTP request. This is usually returned\n * when the HTTP request returns a status code LDO does not recognize.\n */\nexport class UnexpectedHttpError<\n  ResourceType extends Resource,\n> extends HttpErrorResult<ResourceType> {\n  readonly type = \"unexpectedHttpError\" as const;\n}\n\n/**\n * An UnauthenticatedHttpError triggers when a Solid server returns a 401 status\n * indicating that the request is not authenticated.\n */\nexport class UnauthenticatedHttpError<\n  ResourceType extends Resource,\n> extends HttpErrorResult<ResourceType> {\n  readonly type = \"unauthenticatedError\" as const;\n\n  /**\n   * Indicates if a specific response constitutes an UnauthenticatedHttpError\n   * @param response - The request response\n   * @returns true if this response constitutes an UnauthenticatedHttpError\n   */\n  static is(response: Response) {\n    return response.status === 401;\n  }\n}\n\n/**\n * An UnauthenticatedHttpError triggers when a Solid server returns a 403 status\n * indicating that the request is not authorized.\n */\nexport class UnauthorizedHttpError<\n  ResourceType extends Resource,\n> extends HttpErrorResult<ResourceType> {\n  readonly type = \"unauthorizedError\" as const;\n\n  /**\n   * Indicates if a specific response constitutes an UnauthenticatedHttpError\n   * @param response - The request response\n   * @returns true if this response constitutes an UnauthenticatedHttpError\n   */\n  static is(response: Response) {\n    return response.status === 403;\n  }\n}\n\n/**\n * An NotFoundHttpError triggers when a Solid server returns a 404 status. This\n * error is not returned in most cases as a \"absent\" resource is not considered\n * an error, but it is thrown while trying for find a WAC rule for a resource\n * that does not exist.\n */\nexport class NotFoundHttpError<\n  ResourceType extends Resource,\n> extends HttpErrorResult<ResourceType> {\n  readonly type = \"notFoundError\" as const;\n\n  /**\n   * Indicates if a specific response constitutes an NotFoundHttpError\n   * @param response - The request response\n   * @returns true if this response constitutes an NotFoundHttpError\n   */\n  static is(response: Response) {\n    return response.status === 404;\n  }\n}\n\n/**\n * A ServerHttpError triggers when a Solid server returns a 5XX status,\n * indicating that an error happened on the server.\n */\nexport class ServerHttpError<\n  ResourceType extends Resource,\n> extends HttpErrorResult<ResourceType> {\n  readonly type = \"serverError\" as const;\n\n  /**\n   * Indicates if a specific response constitutes a ServerHttpError\n   * @param response - The request response\n   * @returns true if this response constitutes a ServerHttpError\n   */\n  static is(response: Response) {\n    return response.status >= 500 && response.status < 600;\n  }\n}\n"],"mappings":";;;;;AAeA,IAAsB,kBAAtB,MAAsB,wBAEZ,cAA4B;;;;;;CAqBpC,YAAY,UAAwB,UAAoB,SAAkB;AACxE,QACE,UACA,WACE,eAAe,SAAS,IAAI,YAAY,SAAS,OAAO,IAAI,SAAS,WAAW,IACnF;AACD,OAAK,SAAS,SAAS;AACvB,OAAK,UAAU,SAAS;AACxB,OAAK,WAAW;;;;;;;CAQlB,OAAO,KAAK,UAAoB;AAC9B,SACE,EAAE,SAAS,UAAU,OAAO,SAAS,SAAS,QAC9C,SAAS,WAAW,OACpB,SAAS,WAAW;;;;;;;;;CAWxB,OAAO,cACL,UACA,UAC+C;AAC/C,MAAI,gBAAgB,GAAG,SAAS,CAC9B,QAAO,IAAI,gBAAgB,UAAU,SAAS;AAEhD,MAAI,yBAAyB,GAAG,SAAS,CACvC,QAAO,IAAI,yBAAyB,UAAU,SAAS;AAEzD,MAAI,sBAAsB,GAAG,SAAS,CACpC,QAAO,IAAI,sBAAsB,UAAU,SAAS;AAEtD,MAAI,gBAAgB,KAAK,SAAS,CAChC,QAAO,IAAI,oBAAoB,UAAU,SAAS;;;;;;;AAUxD,IAAa,sBAAb,cAEU,gBAA8B;;;AACtC,OAAS,OAAO;;;;;;;AAOlB,IAAa,2BAAb,cAEU,gBAA8B;;;AACtC,OAAS,OAAO;;;;;;;CAOhB,OAAO,GAAG,UAAoB;AAC5B,SAAO,SAAS,WAAW;;;;;;;AAQ/B,IAAa,wBAAb,cAEU,gBAA8B;;;AACtC,OAAS,OAAO;;;;;;;CAOhB,OAAO,GAAG,UAAoB;AAC5B,SAAO,SAAS,WAAW;;;;;;;;;AAU/B,IAAa,oBAAb,cAEU,gBAA8B;;;AACtC,OAAS,OAAO;;;;;;;CAOhB,OAAO,GAAG,UAAoB;AAC5B,SAAO,SAAS,WAAW;;;;;;;AAQ/B,IAAa,kBAAb,cAEU,gBAA8B;;;AACtC,OAAS,OAAO;;;;;;;CAOhB,OAAO,GAAG,UAAoB;AAC5B,SAAO,SAAS,UAAU,OAAO,SAAS,SAAS"}