Skip to content

Commit

Permalink
Fix getTask to return 404 when task isn't found
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenhand committed Nov 28, 2024
1 parent d15b0c0 commit 7924e61
Showing 1 changed file with 20 additions and 8 deletions.
28 changes: 20 additions & 8 deletions functions/getTask.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
success,
functionValidator as TokenValidator,
error403,
send,
} from '@tech-matters/serverless-helpers';

type EnvVars = {
Expand Down Expand Up @@ -92,14 +93,25 @@ export const handler = TokenValidator(
resolve(error400('taskSid is undefined'));
return;
}

const result = await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(event.taskSid)
.fetch();

resolve(success(result));
try {
const result = await context
.getTwilioClient()
.taskrouter.workspaces(context.TWILIO_WORKSPACE_SID)
.tasks(event.taskSid)
.fetch();
resolve(success(result));
} catch (err) {
const error = err as Error;
if (
error.message.match(
/The requested resource \/Workspaces\/WS[a-z0-9]+\/Tasks\/WT[a-z0-9]+ was not found/,
)
) {
resolve(send(404)({ message: error.message }));
return;
}
resolve(error500(error));
}
} catch (err: any) {
resolve(error500(err));
}
Expand Down

0 comments on commit 7924e61

Please sign in to comment.