1- import { NO_CONTENT } from 'http-status-codes' ;
1+ import { BAD_REQUEST , NO_CONTENT , UNAUTHORIZED } from 'http-status-codes' ;
2+ import NoModel from 'jscommons/dist/errors/NoModel' ;
3+ import Unauthorised from 'jscommons/dist/errors/Unauthorised' ;
4+ import { Warnings } from 'rulr' ;
5+ import stringToStream from 'string-to-stream' ;
26import getActivityId from '../../expressPresenter/utils/getActivityId' ;
37import getEtag from '../../expressPresenter/utils/getEtag' ;
48import getProfileId from '../../expressPresenter/utils/getProfileId' ;
59import validateVersionHeader from '../../expressPresenter/utils/validateVersionHeader' ;
6- import { xapiHeaderVersion } from '../../utils/constants' ;
10+ import { jsonContentType , xapiHeaderVersion } from '../../utils/constants' ;
711import { deleteActivityProfile } from './deleteActivityProfile' ;
812import { AuthConfig } from './utils/getAuthConfig/AuthConfig' ;
913import { getClient } from './utils/getClient/getClient' ;
1014import { FileStorageConfig } from './utils/getFileStorageConfig/FileStorageConfig' ;
1115import { MongoRecordStorageConfig } from './utils/getRecordStorageConfig/RecordStorageConfig' ;
1216import { TrackingConfig } from './utils/getTrackingConfig/TrackingConfig' ;
1317import { HttpRequest , HttpResponse } from './utils/HttpInterfaces' ;
18+ import { translateWarning } from './utils/translateWarning' ;
1419
1520export interface DeleteActivityProfileViaHttpConfig {
1621 readonly authConfig : AuthConfig ;
@@ -23,18 +28,61 @@ export async function deleteActivityProfileViaHttp(
2328 config : DeleteActivityProfileViaHttpConfig ,
2429 req : HttpRequest ,
2530) : Promise < HttpResponse > {
26- const client = await getClient ( config , req . headers . authorization ) ;
27- validateVersionHeader ( req . headers [ 'x-experience-api-version' ] ) ;
31+ try {
32+ const client = await getClient ( config , req . headers . authorization ) ;
33+ validateVersionHeader ( req . headers [ 'x-experience-api-version' ] ) ;
2834
29- const ifMatch = getEtag ( req . headers [ 'if-match' ] ) ;
30- const activityId = getActivityId ( req . query . activityId ) ;
31- const profileId = getProfileId ( req . query . profileId ) ;
35+ const ifMatch = getEtag ( req . headers [ 'if-match' ] ) ;
36+ const activityId = getActivityId ( req . query . activityId ) ;
37+ const profileId = getProfileId ( req . query . profileId ) ;
3238
33- await deleteActivityProfile ( config , { activityId, client, profileId, ifMatch } ) ;
34- return {
35- statusCode : NO_CONTENT ,
36- headers : {
37- 'X-Experience-API-Version' : xapiHeaderVersion ,
38- } ,
39- } ;
39+ await deleteActivityProfile ( config , { activityId, client, profileId, ifMatch } ) ;
40+ return {
41+ statusCode : NO_CONTENT ,
42+ headers : {
43+ 'X-Experience-API-Version' : xapiHeaderVersion ,
44+ } ,
45+ } ;
46+ } catch ( err ) {
47+ if ( err instanceof Unauthorised ) {
48+ return {
49+ statusCode : UNAUTHORIZED ,
50+ headers : {
51+ 'X-Experience-API-Version' : xapiHeaderVersion ,
52+ 'Content-Type' : jsonContentType ,
53+ } ,
54+ body : stringToStream ( JSON . stringify ( {
55+ message : 'Unauthorized' ,
56+ requestId : req . requestId ,
57+ } ) ) ,
58+ } ;
59+ }
60+ if ( err instanceof NoModel ) {
61+ return {
62+ statusCode : UNAUTHORIZED ,
63+ headers : {
64+ 'X-Experience-API-Version' : xapiHeaderVersion ,
65+ 'Content-Type' : jsonContentType ,
66+ } ,
67+ body : stringToStream ( JSON . stringify ( {
68+ message : `No ${ err . modelName } found` ,
69+ requestId : req . requestId ,
70+ } ) ) ,
71+ } ;
72+ }
73+ if ( err instanceof Warnings ) {
74+ return {
75+ statusCode : BAD_REQUEST ,
76+ headers : {
77+ 'X-Experience-API-Version' : xapiHeaderVersion ,
78+ 'Content-Type' : jsonContentType ,
79+ } ,
80+ body : stringToStream ( JSON . stringify ( {
81+ warnings : err . warnings . map ( translateWarning ) ,
82+ requestId : req . requestId ,
83+ } ) ) ,
84+ } ;
85+ }
86+ throw err ;
87+ }
4088}
0 commit comments