Skip to content

Commit 190d824

Browse files
authored
fix: Update S3 client usage in BaseConfigService (#1215)
`BaseConfigService` has been broken since upgrading to AWS SDK v3 (#1201). - The S3 object body is no longer returned as a `Buffer`, but as a `StreamingBlobPayloadOutputTypes` - Errors now have a capitalised `Code` key, which impacts handling of missing config objects Jira: [ENG-3796] [ENG-3796]: https://comicrelief.atlassian.net/browse/ENG-3796
1 parent 0721d9e commit 190d824

2 files changed

Lines changed: 16 additions & 10 deletions

File tree

src/services/BaseConfigService.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import DependencyAwareClass from '../core/DependencyAwareClass';
99
import LambdaTermination from '../utils/LambdaTermination';
1010

1111
/**
12-
* `error.code` for S3 404 errors.
12+
* `error.Code` for S3 404 errors.
1313
*/
1414
export const S3_NO_SUCH_KEY_ERROR_CODE = 'NoSuchKey';
1515

@@ -111,7 +111,7 @@ export default class BaseConfigService extends DependencyAwareClass {
111111
const response = await this.client.send(new GetObjectCommand(
112112
(this.constructor as typeof BaseConfigService).s3config,
113113
));
114-
const body = String(response.Body);
114+
const body = await response.Body?.transformToString();
115115

116116
if (!body) {
117117
// Empty strings are not valid configurations
@@ -135,7 +135,7 @@ export default class BaseConfigService extends DependencyAwareClass {
135135
try {
136136
return await this.get();
137137
} catch (error: any) {
138-
if (error.code !== S3_NO_SUCH_KEY_ERROR_CODE) {
138+
if (error.Code !== S3_NO_SUCH_KEY_ERROR_CODE) {
139139
// Throw directly any other error
140140
throw error;
141141
}
@@ -157,7 +157,7 @@ export default class BaseConfigService extends DependencyAwareClass {
157157
try {
158158
base = await this.get();
159159
} catch (error: any) {
160-
if (error.code !== S3_NO_SUCH_KEY_ERROR_CODE) {
160+
if (error.Code !== S3_NO_SUCH_KEY_ERROR_CODE) {
161161
// Throw directly any other error
162162
throw error;
163163
}

tests/unit/services/BaseConfigService.spec.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
DependencyInjection,
1818
} from '@/src';
1919

20-
type ErrorWithCode = Error & { code?: any };
20+
type ErrorWithCode = Error & { Code?: any };
2121

2222
/**
2323
* Generate a BaseConfigService with mock S3 client.
@@ -42,6 +42,12 @@ const getService = (
4242
let result;
4343
if (command instanceof GetObjectCommand) {
4444
result = getObject;
45+
const { Body: body } = result;
46+
if (typeof body === 'string') {
47+
result.Body = {
48+
transformToString: () => Promise.resolve(body),
49+
};
50+
}
4551
} else if (command instanceof PutObjectCommand) {
4652
result = putObject;
4753
} else if (command instanceof DeleteObjectCommand) {
@@ -150,7 +156,7 @@ describe('unit.services.BaseConfigService', () => {
150156

151157
it('propagates the 404', async () => {
152158
const error: ErrorWithCode = new Error('404');
153-
error.code = S3_NO_SUCH_KEY_ERROR_CODE;
159+
error.Code = S3_NO_SUCH_KEY_ERROR_CODE;
154160

155161
const service = getService({ getObject: error });
156162

@@ -161,7 +167,7 @@ describe('unit.services.BaseConfigService', () => {
161167
describe('getOrCreate', () => {
162168
it('uploads the defaultConfig with a 404 error', async () => {
163169
const error: ErrorWithCode = new Error('404');
164-
error.code = S3_NO_SUCH_KEY_ERROR_CODE;
170+
error.Code = S3_NO_SUCH_KEY_ERROR_CODE;
165171

166172
const service = getService({ getObject: error });
167173
const config = await service.getOrCreate();
@@ -171,7 +177,7 @@ describe('unit.services.BaseConfigService', () => {
171177

172178
it('throws any non-404 error', async () => {
173179
const error: ErrorWithCode = new Error('Bad error');
174-
error.code = 'another';
180+
error.Code = 'another';
175181

176182
const service = getService({ getObject: error });
177183

@@ -193,7 +199,7 @@ describe('unit.services.BaseConfigService', () => {
193199

194200
it('uses the base config if no existing config is found', async () => {
195201
const error: ErrorWithCode = new Error('404');
196-
error.code = S3_NO_SUCH_KEY_ERROR_CODE;
202+
error.Code = S3_NO_SUCH_KEY_ERROR_CODE;
197203
const service = getService({ getObject: error });
198204

199205
const existing = service.constructor.defaultConfig;
@@ -206,7 +212,7 @@ describe('unit.services.BaseConfigService', () => {
206212

207213
it('throws any non-404 error', async () => {
208214
const error: ErrorWithCode = new Error('Bad error');
209-
error.code = 'another';
215+
error.Code = 'another';
210216

211217
const service = getService({ getObject: error });
212218

0 commit comments

Comments
 (0)