Merge pull request #4130 from omnivore-app/fix/notion

fix: a better error message on web if notion database is not found
This commit is contained in:
Hongbo Wu
2024-07-01 22:40:50 +08:00
committed by GitHub
4 changed files with 45 additions and 10 deletions

View File

@ -53,7 +53,7 @@ export const setIntegrationResolver = authorized<
SetIntegrationSuccess,
SetIntegrationError,
MutationSetIntegrationArgs
>(async (_, { input }, { uid }) => {
>(async (_, { input }, { uid, log }) => {
const integrationToSave: DeepPartial<Integration> = {
...input,
user: { id: uid },
@ -104,11 +104,28 @@ export const setIntegrationResolver = authorized<
if (settings.parentDatabaseId) {
// update notion database properties
const notion = new NotionClient(integration.token, integration)
try {
await notion.updateDatabase(settings.parentDatabaseId)
const database = await notion.findDatabase(settings.parentDatabaseId)
try {
await notion.updateDatabase(database)
} catch (error) {
log.error('failed to update notion database', {
databaseId: settings.parentDatabaseId,
})
return {
errorCodes: [SetIntegrationErrorCode.BadRequest],
}
}
} catch (error) {
log.error('notion database not found', {
databaseId: settings.parentDatabaseId,
})
return {
errorCodes: [SetIntegrationErrorCode.BadRequest],
errorCodes: [SetIntegrationErrorCode.NotFound],
}
}
}

View File

@ -1,4 +1,5 @@
import { Client } from '@notionhq/client'
import { GetDatabaseResponse } from '@notionhq/client/build/src/api-endpoints'
import axios from 'axios'
import { HighlightType } from '../../entity/highlight'
import { Integration } from '../../entity/integration'
@ -377,14 +378,13 @@ export class NotionClient implements IntegrationClient {
return true
}
private findDatabase = async (databaseId: string) => {
findDatabase = async (databaseId: string) => {
return this.client.databases.retrieve({
database_id: databaseId,
})
}
updateDatabase = async (databaseId: string) => {
const database = await this.findDatabase(databaseId)
updateDatabase = async (database: GetDatabaseResponse) => {
// find the title property and update it
const titleProperty = Object.entries(database.properties).find(
([, property]) => property.type === 'title'

View File

@ -23,9 +23,17 @@ type SetIntegrationResult = {
setIntegration: SetIntegrationData
}
export enum SetIntegrationErrorCode {
AlreadyExists = 'ALREADY_EXISTS',
BadRequest = 'BAD_REQUEST',
InvalidToken = 'INVALID_TOKEN',
NotFound = 'NOT_FOUND',
Unauthorized = 'UNAUTHORIZED',
}
type SetIntegrationData = {
integration: Integration
errorCodes?: string[]
errorCodes?: SetIntegrationErrorCode[]
}
type Integration = {
@ -66,8 +74,8 @@ export async function setIntegrationMutation(
const data = (await gqlFetcher(mutation, { input })) as SetIntegrationResult
const error = data.setIntegration.errorCodes?.find(() => true)
if (error) {
if (error === 'INVALID_TOKEN') throw 'Your token is invalid.'
throw error
throw new Error(error)
}
return data.setIntegration.integration
}

View File

@ -13,7 +13,10 @@ import {
Task,
TaskState,
} from '../../../lib/networking/mutations/exportToIntegrationMutation'
import { setIntegrationMutation } from '../../../lib/networking/mutations/setIntegrationMutation'
import {
SetIntegrationErrorCode,
setIntegrationMutation,
} from '../../../lib/networking/mutations/setIntegrationMutation'
import { apiFetcher } from '../../../lib/networking/networkHelpers'
import { useGetIntegrationQuery } from '../../../lib/networking/queries/useGetIntegrationQuery'
import { showSuccessToast } from '../../../lib/toastHelpers'
@ -86,6 +89,13 @@ export default function Notion(): JSX.Element {
revalidate()
messageApi.success('Notion settings updated successfully.')
} catch (error) {
if (
error instanceof Error &&
error.message === SetIntegrationErrorCode.NotFound
) {
return messageApi.error('Notion database not found. Please make sure if you are using database ID instead of page ID.')
}
messageApi.error('There was an error updating Notion settings.')
}
}