Skip to content

Commit

Permalink
fix(chat): handle API errors (Issue #663) (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
IlyaBondar authored Feb 14, 2024
1 parent bc934ad commit 1a5f2bc
Show file tree
Hide file tree
Showing 13 changed files with 503 additions and 189 deletions.
19 changes: 18 additions & 1 deletion apps/chat/src/pages/api/[entitytype]/[...slug].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { getServerSession } from 'next-auth';
import { JWT, getToken } from 'next-auth/jwt';

import { constructPath } from '@/src/utils/app/file';
import { validateServerSession } from '@/src/utils/auth/session';
import { OpenAIError } from '@/src/utils/server';
import {
encodeSlugs,
getEntityTypeFromPath,
getEntityUrlFromSlugs,
isValidEntityApiType,
} from '@/src/utils/server/api';
import { getApiHeaders } from '@/src/utils/server/get-headers';
Expand All @@ -19,6 +20,22 @@ import { authOptions } from '@/src/pages/api/auth/[...nextauth]';
import fetch from 'node-fetch';
import { Readable } from 'stream';

const getEntityUrlFromSlugs = (
dialApiHost: string,
req: NextApiRequest,
): string => {
const entityType = getEntityTypeFromPath(req);
const slugs = Array.isArray(req.query.slug)
? req.query.slug
: [req.query.slug];

if (!slugs || slugs.length === 0) {
throw new OpenAIError(`No ${entityType} path provided`, '', '', '400');
}

return constructPath(dialApiHost, 'v1', entityType, encodeSlugs(slugs));
};

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const entityType = getEntityTypeFromPath(req);
if (!entityType || !isValidEntityApiType(entityType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ import { NextApiRequest, NextApiResponse } from 'next';
import { getToken } from 'next-auth/jwt';
import { getServerSession } from 'next-auth/next';

import { constructPath } from '@/src/utils/app/file';
import { validateServerSession } from '@/src/utils/auth/session';
import { OpenAIError } from '@/src/utils/server';
import {
ApiKeys,
encodeApiUrl,
getEntityTypeFromPath,
isValidEntityApiType,
} from '@/src/utils/server/api';
import { encodeSlugs } from '@/src/utils/server/api';
import { getApiHeaders } from '@/src/utils/server/get-headers';
import { logger } from '@/src/utils/server/logger';

Expand All @@ -27,11 +23,6 @@ import { authOptions } from '@/src/pages/api/auth/[...nextauth]';
import fetch from 'node-fetch';

const handler = async (req: NextApiRequest, res: NextApiResponse) => {
const entityType = getEntityTypeFromPath(req);
if (!entityType || !isValidEntityApiType(entityType)) {
return res.status(400).json(errorsMessages.notValidEntityType);
}

const session = await getServerSession(req, res, authOptions);
const isSessionValid = validateServerSession(session, req, res);
if (!isSessionValid) {
Expand All @@ -40,22 +31,28 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {

try {
const {
path = '',
filter,
bucket,
recursive = false,
limit = 1000,
} = req.query as {
path: string;
filter?: BackendDataNodeType;
bucket: string;
recursive?: string;
limit?: number;
};

const token = await getToken({ req });
const slugs = Array.isArray(req.query.listing)
? req.query.listing
: [req.query.listing];

if (!slugs || slugs.length === 0) {
throw new OpenAIError(`No path provided`, '', '', '400');
}

const url = `${
process.env.DIAL_API_HOST
}/v1/metadata/${path ? `${encodeApiUrl(path)}` : `${entityType}/${bucket}`}/?limit=1000${recursive ? '&recursive=true' : ''}`;
const url = `${constructPath(
process.env.DIAL_API_HOST,
'v1/metadata',
encodeSlugs(slugs),
)}/?limit=${limit}&recursive=${recursive}`;

const response = await fetch(url, {
headers: getApiHeaders({ jwt: token?.access_token as string }),
Expand All @@ -78,8 +75,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
| BackendChatFolder
)[] = json.items || [];

const filterableEntityTypes: string[] = Object.values(ApiKeys);
if (filter && filterableEntityTypes.includes(entityType)) {
if (filter) {
result = result.filter((item) => item.nodeType === filter);
}

Expand Down
Loading

0 comments on commit 1a5f2bc

Please sign in to comment.