Files
supabase/apps/docs/app/api/graphql/tests/errors.test.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

65 lines
1.6 KiB
TypeScript

import { describe, expect, it, vi } from 'vitest'
import { POST } from '../route'
describe('/api/graphql error', () => {
it('returns the error matching given error code and service', async () => {
const errorQuery = `
query {
error(code: "test_code", service: AUTH) {
code
service
httpStatusCode
message
}
}
`
const request = new Request('http://localhost/api/graphql', {
method: 'POST',
body: JSON.stringify({ query: errorQuery }),
})
const result = await POST(request)
const {
data: { error },
errors,
} = await result.json()
expect(errors).toBeUndefined()
expect(error.code).toBe('test_code')
expect(error.service).toBe('AUTH')
expect(error.httpStatusCode).toBe(500)
expect(error.message).toBe('This is a test error message')
})
it('returns error if no matching error exists', async () => {
vi.spyOn(console, 'error').mockImplementation(() => {})
const errorQuery = `
query {
error(code: "nonexistent_code", service: AUTH) {
code
service
httpStatusCode
message
}
}
`
const request = new Request('http://localhost/api/graphql', {
method: 'POST',
body: JSON.stringify({ query: errorQuery }),
})
const result = await POST(request)
const {
data: { error },
errors,
} = await result.json()
expect(error).toBe(null)
expect(errors).toHaveLength(1)
expect(errors[0].message).toMatch(/not found/i)
vi.restoreAllMocks()
})
})