Skip to content

Commit 005528a

Browse files
authored
fix(server): http error parsing on endpoints without a default response (#12927)
1 parent 8d515ad commit 005528a

File tree

13 files changed

+162
-18
lines changed

13 files changed

+162
-18
lines changed

Diff for: mobile/openapi/README.md

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: mobile/openapi/lib/api.dart

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: mobile/openapi/lib/api/notifications_api.dart

+9-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: mobile/openapi/lib/api_client.dart

+2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: mobile/openapi/lib/model/test_email_response_dto.dart

+99
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: open-api/immich-openapi-specs.json

+18
Original file line numberDiff line numberDiff line change
@@ -3491,6 +3491,13 @@
34913491
},
34923492
"responses": {
34933493
"200": {
3494+
"content": {
3495+
"application/json": {
3496+
"schema": {
3497+
"$ref": "#/components/schemas/TestEmailResponseDto"
3498+
}
3499+
}
3500+
},
34943501
"description": ""
34953502
}
34963503
},
@@ -12348,6 +12355,17 @@
1234812355
},
1234912356
"type": "object"
1235012357
},
12358+
"TestEmailResponseDto": {
12359+
"properties": {
12360+
"messageId": {
12361+
"type": "string"
12362+
}
12363+
},
12364+
"required": [
12365+
"messageId"
12366+
],
12367+
"type": "object"
12368+
},
1235112369
"TimeBucketResponseDto": {
1235212370
"properties": {
1235312371
"count": {

Diff for: open-api/typescript-sdk/src/fetch-client.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -656,6 +656,9 @@ export type SystemConfigSmtpDto = {
656656
replyTo: string;
657657
transport: SystemConfigSmtpTransportDto;
658658
};
659+
export type TestEmailResponseDto = {
660+
messageId: string;
661+
};
659662
export type OAuthConfigDto = {
660663
redirectUri: string;
661664
};
@@ -2220,7 +2223,10 @@ export function addMemoryAssets({ id, bulkIdsDto }: {
22202223
export function sendTestEmail({ systemConfigSmtpDto }: {
22212224
systemConfigSmtpDto: SystemConfigSmtpDto;
22222225
}, opts?: Oazapfts.RequestOpts) {
2223-
return oazapfts.ok(oazapfts.fetchText("/notifications/test-email", oazapfts.json({
2226+
return oazapfts.ok(oazapfts.fetchJson<{
2227+
status: 200;
2228+
data: TestEmailResponseDto;
2229+
}>("/notifications/test-email", oazapfts.json({
22242230
...opts,
22252231
method: "POST",
22262232
body: systemConfigSmtpDto

Diff for: server/src/controllers/notification.controller.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Body, Controller, HttpCode, HttpStatus, Post } from '@nestjs/common';
22
import { ApiTags } from '@nestjs/swagger';
33
import { AuthDto } from 'src/dtos/auth.dto';
4+
import { TestEmailResponseDto } from 'src/dtos/notification.dto';
45
import { SystemConfigSmtpDto } from 'src/dtos/system-config.dto';
56
import { Auth, Authenticated } from 'src/middleware/auth.guard';
67
import { NotificationService } from 'src/services/notification.service';
@@ -13,7 +14,7 @@ export class NotificationController {
1314
@Post('test-email')
1415
@HttpCode(HttpStatus.OK)
1516
@Authenticated({ admin: true })
16-
sendTestEmail(@Auth() auth: AuthDto, @Body() dto: SystemConfigSmtpDto) {
17+
sendTestEmail(@Auth() auth: AuthDto, @Body() dto: SystemConfigSmtpDto): Promise<TestEmailResponseDto> {
1718
return this.service.sendTestEmail(auth.user.id, dto);
1819
}
1920
}

Diff for: server/src/dtos/notification.dto.ts

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export class TestEmailResponseDto {
2+
messageId!: string;
3+
}

Diff for: server/src/services/notification.service.spec.ts

-5
Original file line numberDiff line numberDiff line change
@@ -616,11 +616,6 @@ describe(NotificationService.name, () => {
616616
await expect(sut.handleSendEmail({ html: '', subject: '', text: '', to: '' })).resolves.toBe(JobStatus.SKIPPED);
617617
});
618618

619-
it('should fail if email could not be sent', async () => {
620-
systemMock.get.mockResolvedValue({ notifications: { smtp: { enabled: true } } });
621-
await expect(sut.handleSendEmail({ html: '', subject: '', text: '', to: '' })).resolves.toBe(JobStatus.FAILED);
622-
});
623-
624619
it('should send mail successfully', async () => {
625620
systemMock.get.mockResolvedValue({ notifications: { smtp: { enabled: true, from: '[email protected]' } } });
626621
notificationMock.sendEmail.mockResolvedValue({ messageId: '', response: '' });

Diff for: server/src/services/notification.service.ts

+5-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HttpException, HttpStatus, Inject, Injectable } from '@nestjs/common';
1+
import { BadRequestException, Inject, Injectable } from '@nestjs/common';
22
import { DEFAULT_EXTERNAL_DOMAIN } from 'src/constants';
33
import { SystemConfigCore } from 'src/cores/system-config.core';
44
import { OnEmit } from 'src/decorators';
@@ -140,7 +140,7 @@ export class NotificationService {
140140
try {
141141
await this.notificationRepository.verifySmtp(dto.transport);
142142
} catch (error) {
143-
throw new HttpException('Failed to verify SMTP configuration', HttpStatus.BAD_REQUEST, { cause: error });
143+
throw new BadRequestException('Failed to verify SMTP configuration', { cause: error });
144144
}
145145

146146
const { server } = await this.configCore.getConfig({ withCache: false });
@@ -152,7 +152,7 @@ export class NotificationService {
152152
},
153153
});
154154

155-
await this.notificationRepository.sendEmail({
155+
const { messageId } = await this.notificationRepository.sendEmail({
156156
to: user.email,
157157
subject: 'Test email from Immich',
158158
html,
@@ -161,6 +161,8 @@ export class NotificationService {
161161
replyTo: dto.replyTo || dto.from,
162162
smtp: dto.transport,
163163
});
164+
165+
return { messageId };
164166
}
165167

166168
async handleUserSignup({ id, tempPassword }: INotifySignupJob) {
@@ -312,10 +314,6 @@ export class NotificationService {
312314
imageAttachments: data.imageAttachments,
313315
});
314316

315-
if (!response) {
316-
return JobStatus.FAILED;
317-
}
318-
319317
this.logger.log(`Sent mail with id: ${response.messageId} status: ${response.response}`);
320318

321319
return JobStatus.SUCCESS;

Diff for: server/test/repositories/notification.repository.mock.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { Mocked } from 'vitest';
44
export const newNotificationRepositoryMock = (): Mocked<INotificationRepository> => {
55
return {
66
renderEmail: vitest.fn(),
7-
sendEmail: vitest.fn(),
7+
sendEmail: vitest.fn().mockResolvedValue({ messageId: 'message-1' }),
88
verifySmtp: vitest.fn(),
99
};
1010
};

Diff for: web/src/lib/utils/handle-error.ts

+14-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,21 @@ import { isHttpError } from '@immich/sdk';
22
import { notificationController, NotificationType } from '../components/shared-components/notification/notification';
33

44
export function getServerErrorMessage(error: unknown) {
5-
if (isHttpError(error)) {
6-
return error.data?.message || error.message;
5+
if (!isHttpError(error)) {
6+
return;
77
}
8+
9+
// errors for endpoints without return types aren't parsed as json
10+
let data = error.data;
11+
if (typeof data === 'string') {
12+
try {
13+
data = JSON.parse(data);
14+
} catch {
15+
// Not a JSON string
16+
}
17+
}
18+
19+
return data?.message || error.message;
820
}
921

1022
export function handleError(error: unknown, message: string) {

0 commit comments

Comments
 (0)