Files
supabase/apps/docs/resources/error/errorModel.ts
Charis aba0095bd7 feat(content api): add error endpoint (#35941)
* feat(content api): add error endpoint

Add an endpoint to return the details of a Supabase error, given the
error code and service.

Schema additions:

```graphql
type RootQueryType {
  "...previous root queries"

  """Get the details of an error code returned from a Supabase service"""
  error(code: String!, service: Service!): Error
}

"""An error returned by a Supabase service"""
type Error {
  """
  The unique code identifying the error. The code is stable, and can be used for string matching during error handling.
  """
  code: String!

  """The Supabase service that returns this error."""
  service: Service!

  """The HTTP status code returned with this error."""
  httpStatusCode: Int

  """
  A human-readable message describing the error. The message is not stable, and should not be used for string matching during error handling. Use the code instead.
  """
  message: String
}

enum Service {
  AUTH
  REALTIME
  STORAGE
}
```

* test(content api): add tests for top-level query `error`
2025-05-29 15:39:47 -04:00

75 lines
1.7 KiB
TypeScript

import { ApiErrorGeneric, convertPostgrestToApiError, NoDataError } from '~/app/api/utils'
import { Result } from '~/features/helpers.fn'
import { supabase } from '~/lib/supabase'
export const SERVICES = {
AUTH: {
value: 'AUTH',
},
REALTIME: {
value: 'REALTIME',
},
STORAGE: {
value: 'STORAGE',
},
} as const
type Service = keyof typeof SERVICES
export class ErrorModel {
public code: string
public service: Service
public httpStatusCode?: number
public message?: string
constructor({
code,
service,
httpStatusCode: httpStatusCode,
message,
}: {
code: string
service: Service
httpStatusCode?: number
message?: string
}) {
this.code = code
this.service = service
this.httpStatusCode = httpStatusCode
this.message = message
}
static async loadSingleError({
code,
service,
}: {
code: string
service: Service
}): Promise<Result<ErrorModel, ApiErrorGeneric>> {
return new Result(
await supabase()
.schema('content')
.from('error')
.select('code, ...service(service:name), httpStatusCode:http_status_code, message')
.eq('code', code)
.eq('service.name', service)
.is('deleted_at', null)
.single<{
code: string
service: Service
httpStatusCode?: number
message?: string
}>()
)
.map((data) => {
return new ErrorModel(data)
})
.mapError((error) => {
if (error.code === 'PGRST116') {
return new NoDataError('Error for given code and service does not exist', error)
}
return convertPostgrestToApiError(error)
})
}
}