File size: 1,420 Bytes
80f4e3d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import { HttpStatus } from '@api/routes/index.router';

export interface MetaErrorResponse {
  status: number;
  error: string;
  message: string;
  details: {
    whatsapp_error: string;
    whatsapp_code: string | number;
    error_user_title: string;
    error_user_msg: string;
    error_type: string;
    error_subcode: number | null;
    fbtrace_id: string | null;
    context: string;
    type: string;
  };
  timestamp: string;
}

/**
 * Creates standardized error response for Meta/WhatsApp API errors
 */
export function createMetaErrorResponse(error: any, context: string): MetaErrorResponse {
  // Extract Meta/WhatsApp specific error fields
  const metaError = error.template || error;
  const errorUserTitle = metaError.error_user_title || metaError.message || 'Unknown error';
  const errorUserMsg = metaError.error_user_msg || metaError.message || 'Unknown error';

  return {
    status: HttpStatus.BAD_REQUEST,
    error: 'Bad Request',
    message: errorUserTitle,
    details: {
      whatsapp_error: errorUserMsg,
      whatsapp_code: metaError.code || 'UNKNOWN_ERROR',
      error_user_title: errorUserTitle,
      error_user_msg: errorUserMsg,
      error_type: metaError.type || 'UNKNOWN',
      error_subcode: metaError.error_subcode || null,
      fbtrace_id: metaError.fbtrace_id || null,
      context,
      type: 'whatsapp_api_error',
    },
    timestamp: new Date().toISOString(),
  };
}