Skip to content

Commit

Permalink
fix: reduce errors caused by lack of Notion token
Browse files Browse the repository at this point in the history
In the new UI there is a button to disconnect notion and that would
likely have increased these errors.

Closes: #1521
  • Loading branch information
aalemayhu committed Aug 11, 2024
1 parent 7b0d8f3 commit ed84d09
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/data_layer/NotionRespository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ import NotionTokens from './public/NotionTokens';
import unHashToken from '../lib/misc/unHashToken';

export interface INotionRepository {
getNotionData(owner: number | string): Promise<NotionTokens>;
getNotionData(owner: number | string): Promise<NotionTokens | null>;
saveNotionToken(
user: number,
data: { [key: string]: string },
hash: (token: string) => string
): Promise<boolean>;
getNotionToken(owner: string): Promise<string>;
getNotionToken(owner: string): Promise<string | null>;
deleteBlocksByOwner(owner: number): Promise<number>;
deleteNotionData(owner: number): Promise<boolean>;
}
Expand All @@ -21,7 +21,11 @@ class NotionRepository implements INotionRepository {

constructor(private readonly database: Knex) {}

getNotionData(owner: number | string): Promise<NotionTokens> {
getNotionData(owner: number | string): Promise<NotionTokens | null> {
if (!owner) {
return Promise.resolve(null);
}

return this.database(this.notionTokensTable)
.where({ owner: owner })
.returning(['token', 'workspace_name'])
Expand All @@ -41,7 +45,7 @@ class NotionRepository implements INotionRepository {
workspace_name: data.workspace_name,
workspace_icon: data.workspace_icon,
workspace_id: data.workspace_id,
notion_owner: data.owner,
notion_owner: data.owner, // This actually JSON blob from Notion and not related to our owner id
token: hash(data.access_token),
owner: user,
})
Expand All @@ -64,16 +68,17 @@ class NotionRepository implements INotionRepository {
* @param owner user id
* @returns unhashed token
*/
async getNotionToken(owner: string): Promise<string> {
async getNotionToken(owner: string): Promise<string | null> {
const row = await this.database('notion_tokens')
.where({ owner })
.returning('token')
.first();

/**
* The user can disconnect Notion at any point so we should not throw an error here.
*/
if (!row) {
throw new Error(
`Could not find your Notion token. Please report this issue with your userid: ${owner}`
);
return Promise.resolve(null);
}

return unHashToken(row.token);
Expand Down
5 changes: 5 additions & 0 deletions src/services/NotionService/NotionService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { APIErrorCode } from '@notionhq/client';
import axios from 'axios';


Check failure on line 4 in src/services/NotionService/NotionService.ts

View workflow job for this annotation

GitHub Actions / build (18.16.1)

Delete `⏎`
import { INotionRepository } from '../../data_layer/NotionRespository';
import { sendError } from '../../lib/error/sendError';
import hashToken from '../../lib/misc/hashToken';
Expand Down Expand Up @@ -41,6 +43,9 @@ export class NotionService {

getNotionAPI = async (owner: string): Promise<NotionAPIWrapper> => {
const token = await this.notionRepository.getNotionToken(owner);
if (!token) {
throw new Error(APIErrorCode.Unauthorized);
}
return new NotionAPIWrapper(token!, owner);
};

Expand Down

0 comments on commit ed84d09

Please sign in to comment.