diff --git a/hrm-domain/lambdas/search-index-consumer/payloadToIndex.ts b/hrm-domain/lambdas/search-index-consumer/payloadToIndex.ts index 2a161091b..7317f22f6 100644 --- a/hrm-domain/lambdas/search-index-consumer/payloadToIndex.ts +++ b/hrm-domain/lambdas/search-index-consumer/payloadToIndex.ts @@ -24,7 +24,7 @@ import type { PayloadsByAccountSid, PayloadsByIndex, } from './messagesToPayloads'; -import { type HrmAccountId, newErr, newOkFromData } from '@tech-matters/types'; +import { type HrmAccountId, newErr, newOkFromData, isErr } from '@tech-matters/types'; import { HrmIndexProcessorError } from '@tech-matters/job-errors'; const handleIndexPayload = @@ -90,6 +90,18 @@ const handleIndexPayload = id: documentId.toString(), }); + if (isErr(result)) { + // bubble error if it's different from "not found" + if (result.extraProperties?.statusCode !== 404) { + return result.unwrap(); + } + + console.info( + 'handleIndexPayload: delete operation resulted in 404, ignoring message with id', + messageId, + ); + } + return { accountSid, indexType, diff --git a/packages/elasticsearch-client/src/deleteDocument.ts b/packages/elasticsearch-client/src/deleteDocument.ts index de54a52e8..fcc5032c3 100644 --- a/packages/elasticsearch-client/src/deleteDocument.ts +++ b/packages/elasticsearch-client/src/deleteDocument.ts @@ -15,23 +15,34 @@ */ import { DeleteResponse } from '@elastic/elasticsearch/lib/api/types'; import { PassThroughConfig } from './client'; +import { newErr, newOk, TResult } from '@tech-matters/types'; export type DeleteDocumentExtraParams = { id: string; }; export type DeleteDocumentParams = PassThroughConfig & DeleteDocumentExtraParams; -export type DeleteDocumentResponse = DeleteResponse; +export type DeleteDocumentResponse = TResult<'DeleteDocumentError', DeleteResponse>; export const deleteDocument = async ({ client, id, index, }: DeleteDocumentParams): Promise => { - return client.delete({ - index, - id, - }); + try { + const response = await client.delete({ + index, + id, + }); + + return newOk({ data: response }); + } catch (error) { + return newErr({ + error: 'DeleteDocumentError', + message: error instanceof Error ? error.message : String(error), + extraProperties: { ...(error as any)?.meta, originalError: error }, + }); + } }; export default deleteDocument; diff --git a/packages/types/Result.ts b/packages/types/Result.ts index bdfcaff06..6ae9585fb 100644 --- a/packages/types/Result.ts +++ b/packages/types/Result.ts @@ -29,6 +29,7 @@ export type ErrorResult = ResultBase & { status: 'error'; message: string; error: TError; + extraProperties?: Record; readonly unwrap: () => never; }; @@ -51,12 +52,14 @@ class ResultError> extends Error { export const newErr = ({ message, error, + extraProperties, }: NewErrorResultParams): ErrorResult => { return { _tag: 'Result', status: 'error', message, error, + extraProperties, unwrap: function (this: ErrorResult) { throw new ResultError(this); },