|
| 1 | +import { HttpErrorResponse } from '@angular/common/http' |
| 2 | + |
| 3 | +type HttpErrorContext = { |
| 4 | + browserSupport: string |
| 5 | + csrf: string |
| 6 | + xsrfCookiePresent?: boolean |
| 7 | + authXsrfCookiePresent?: boolean |
| 8 | + csrfCookieState?: 'both' | 'xsrf_only' | 'auth_only' | 'none' |
| 9 | + currentOrigin?: string |
| 10 | + currentHost?: string |
| 11 | + currentPath?: string |
| 12 | + referrerHost?: string |
| 13 | + isOnline?: boolean |
| 14 | +} |
| 15 | + |
| 16 | +type StatusZeroCause = 'offline' | 'aborted' | 'network_or_cors' | 'unknown' |
| 17 | +const MAX_ERROR_BODY_CHARS = 1000 |
| 18 | + |
| 19 | +export function httpErrorEventAttrs( |
| 20 | + error: HttpErrorResponse, |
| 21 | + context: HttpErrorContext |
| 22 | +): Record<string, unknown> { |
| 23 | + const attrs: Record<string, unknown> = { |
| 24 | + status: error.status, |
| 25 | + statusText: error.statusText, |
| 26 | + url: error.url, |
| 27 | + name: error.name, |
| 28 | + browserSupport: context.browserSupport, |
| 29 | + csrf: context.csrf, |
| 30 | + xsrfCookiePresent: context.xsrfCookiePresent, |
| 31 | + authXsrfCookiePresent: context.authXsrfCookiePresent, |
| 32 | + csrfCookieState: context.csrfCookieState, |
| 33 | + requestPath: requestPath(error.url, context.currentOrigin), |
| 34 | + requestHost: requestHost(error.url, context.currentOrigin), |
| 35 | + requestOriginType: requestOriginType(error.url, context.currentOrigin), |
| 36 | + currentPath: context.currentPath, |
| 37 | + currentHost: context.currentHost, |
| 38 | + referrerHost: context.referrerHost, |
| 39 | + isOnline: context.isOnline, |
| 40 | + errorBody: errorBody(error.error), |
| 41 | + errorBodyType: errorBodyType(error.error), |
| 42 | + } |
| 43 | + |
| 44 | + const zeroCause = statusZeroCause(error, context.isOnline) |
| 45 | + if (zeroCause) { |
| 46 | + attrs.statusZeroCause = zeroCause |
| 47 | + } |
| 48 | + |
| 49 | + return attrs |
| 50 | +} |
| 51 | + |
| 52 | +function statusZeroCause( |
| 53 | + error: HttpErrorResponse, |
| 54 | + isOnline: boolean | undefined |
| 55 | +): StatusZeroCause | undefined { |
| 56 | + if (error.status !== 0) { |
| 57 | + return undefined |
| 58 | + } |
| 59 | + |
| 60 | + if (isOnline === false) { |
| 61 | + return 'offline' |
| 62 | + } |
| 63 | + |
| 64 | + const body = error.error as any |
| 65 | + if (body && typeof body === 'object') { |
| 66 | + if (typeof body.type === 'string' && body.type.toLowerCase() === 'abort') { |
| 67 | + return 'aborted' |
| 68 | + } |
| 69 | + if ('isTrusted' in body) { |
| 70 | + return 'network_or_cors' |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (/abort/i.test(error.message || '')) { |
| 75 | + return 'aborted' |
| 76 | + } |
| 77 | + |
| 78 | + if (isOnline === true) { |
| 79 | + return 'network_or_cors' |
| 80 | + } |
| 81 | + return 'unknown' |
| 82 | +} |
| 83 | + |
| 84 | +function requestPath( |
| 85 | + url: string | null, |
| 86 | + currentOrigin: string | undefined |
| 87 | +): string | undefined { |
| 88 | + const parsed = parseUrl(url, currentOrigin) |
| 89 | + return parsed?.pathname |
| 90 | +} |
| 91 | + |
| 92 | +function requestHost( |
| 93 | + url: string | null, |
| 94 | + currentOrigin: string | undefined |
| 95 | +): string | undefined { |
| 96 | + const parsed = parseUrl(url, currentOrigin) |
| 97 | + return parsed?.host |
| 98 | +} |
| 99 | + |
| 100 | +function requestOriginType( |
| 101 | + url: string | null, |
| 102 | + currentOrigin: string | undefined |
| 103 | +): 'same_origin' | 'cross_origin' | 'invalid_or_missing' { |
| 104 | + const parsed = parseUrl(url, currentOrigin) |
| 105 | + if (!parsed) { |
| 106 | + return 'invalid_or_missing' |
| 107 | + } |
| 108 | + if (!currentOrigin) { |
| 109 | + return 'invalid_or_missing' |
| 110 | + } |
| 111 | + return parsed.origin === currentOrigin ? 'same_origin' : 'cross_origin' |
| 112 | +} |
| 113 | + |
| 114 | +function parseUrl( |
| 115 | + url: string | null, |
| 116 | + currentOrigin: string | undefined |
| 117 | +): URL | undefined { |
| 118 | + if (!url) { |
| 119 | + return undefined |
| 120 | + } |
| 121 | + try { |
| 122 | + if (currentOrigin) { |
| 123 | + return new URL(url, currentOrigin) |
| 124 | + } |
| 125 | + return new URL(url) |
| 126 | + } catch { |
| 127 | + return undefined |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +function errorBodyType(value: unknown): string { |
| 132 | + if (value == null || value === '') { |
| 133 | + return 'none' |
| 134 | + } |
| 135 | + if (typeof value === 'string') { |
| 136 | + return 'string' |
| 137 | + } |
| 138 | + if (value instanceof ProgressEvent) { |
| 139 | + return 'progress_event' |
| 140 | + } |
| 141 | + if (Array.isArray(value)) { |
| 142 | + return 'array' |
| 143 | + } |
| 144 | + if (typeof value === 'object') { |
| 145 | + const ctor = (value as object).constructor?.name |
| 146 | + return ctor ? `object:${ctor}` : 'object' |
| 147 | + } |
| 148 | + return typeof value |
| 149 | +} |
| 150 | + |
| 151 | +function errorBody(value: unknown): string | undefined { |
| 152 | + if (value == null || value === '') { |
| 153 | + return undefined |
| 154 | + } |
| 155 | + if (typeof value === 'string') { |
| 156 | + return truncate(value, MAX_ERROR_BODY_CHARS) |
| 157 | + } |
| 158 | + if (value instanceof ProgressEvent) { |
| 159 | + return truncate( |
| 160 | + JSON.stringify({ |
| 161 | + type: value.type, |
| 162 | + isTrusted: value.isTrusted, |
| 163 | + }), |
| 164 | + MAX_ERROR_BODY_CHARS |
| 165 | + ) |
| 166 | + } |
| 167 | + if (typeof value === 'object') { |
| 168 | + try { |
| 169 | + return truncate(JSON.stringify(value), MAX_ERROR_BODY_CHARS) |
| 170 | + } catch { |
| 171 | + return undefined |
| 172 | + } |
| 173 | + } |
| 174 | + return truncate(String(value), MAX_ERROR_BODY_CHARS) |
| 175 | +} |
| 176 | + |
| 177 | +function truncate(value: string, maxChars: number): string { |
| 178 | + if (value.length <= maxChars) { |
| 179 | + return value |
| 180 | + } |
| 181 | + return `${value.slice(0, maxChars)}...[truncated]` |
| 182 | +} |
0 commit comments