Skip to content

[Conversations - Fragment] Return 400 on image format not supported #11451

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion front/lib/api/assistant/conversation/content_fragment.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ProcessAndStoreFileError } from "@app/lib/api/files/upload";
import { processAndStoreFile } from "@app/lib/api/files/upload";
import type { Authenticator } from "@app/lib/auth";
import { FileResource } from "@app/lib/resources/file_resource";
Expand Down Expand Up @@ -38,7 +39,9 @@ export async function toFileContentFragment(
contentFragment: ContentFragmentInputWithContentType;
fileName?: string;
}
): Promise<Result<ContentFragmentInputWithFileIdType, { message: string }>> {
): Promise<
Result<ContentFragmentInputWithFileIdType, ProcessAndStoreFileError>
> {
const file = await FileResource.makeNew({
contentType: contentFragment.contentType,
fileName:
Expand All @@ -58,8 +61,10 @@ export async function toFileContentFragment(

if (processRes.isErr()) {
return new Err({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we avoid raw object as error 🙏 ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case, unfortunately, no, or it would require too much work to make old code consistent, change the DustError type from which ProcessAndStoreFileError is derived, etc.

name: "dust_error",
message:
`Error creating file for content fragment: ` + processRes.error.message,
code: processRes.error.code,
});
}

Expand Down
31 changes: 16 additions & 15 deletions front/lib/api/files/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ const resizeAndUploadToFileStorage: ProcessingFunction = async (

try {
await pipeline(readStream, resizedImageStream, writeStream);

return new Ok(undefined);
} catch (err) {
logger.error(
Expand Down Expand Up @@ -333,25 +332,21 @@ const maybeApplyProcessing: ProcessingFunction = async (
}
};

export type ProcessAndStoreFileError = Omit<DustError, "code"> & {
code:
| "internal_server_error"
| "invalid_request_error"
| "file_too_large"
| "file_type_not_supported"
| "file_is_empty";
};
export async function processAndStoreFile(
auth: Authenticator,
{
file,
reqOrString,
}: { file: FileResource; reqOrString: IncomingMessage | string }
): Promise<
Result<
FileResource,
Omit<DustError, "code"> & {
code:
| "internal_server_error"
| "invalid_request_error"
| "file_too_large"
| "file_type_not_supported"
| "file_is_empty";
}
>
> {
): Promise<Result<FileResource, ProcessAndStoreFileError>> {
if (file.isReady || file.isFailed) {
return new Err({
name: "dust_error",
Expand Down Expand Up @@ -389,10 +384,16 @@ export async function processAndStoreFile(
const processingRes = await maybeApplyProcessing(auth, file);
if (processingRes.isErr()) {
await file.markAsFailed();
// Unfortunately, there is no better way to catch this image format error.
const code = processingRes.error.message.includes(
"Input buffer contains unsupported image format"
)
? "file_type_not_supported"
: "internal_server_error";

return new Err({
name: "dust_error",
code: "invalid_request_error",
code,
message: `Failed to process the file : ${processingRes.error}`,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,15 @@ async function handler(
contentFragment,
});
if (contentFragmentRes.isErr()) {
if (contentFragmentRes.error.code === "file_type_not_supported") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: contentFragmentRes.error.message,
},
});
}
throw new Error(contentFragmentRes.error.message);
}
contentFragment = contentFragmentRes.value;
Expand Down
9 changes: 9 additions & 0 deletions front/pages/api/v1/w/[wId]/assistant/conversations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,15 @@ async function handler(
contentFragment,
});
if (contentFragmentRes.isErr()) {
if (contentFragmentRes.error.code === "file_type_not_supported") {
return apiError(req, res, {
status_code: 400,
api_error: {
type: "invalid_request_error",
message: contentFragmentRes.error.message,
},
});
}
throw new Error(contentFragmentRes.error.message);
}
contentFragment = contentFragmentRes.value;
Expand Down