Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | 1x 1x 1x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 34x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 8x 34x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 34x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 10x 34x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 34x 7x 7x 7x 7x 7x 7x 7x 6x 1x 7x 7x 7x 7x 7x 7x 7x 34x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 34x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 44x 44x 1x 1x 3x 3x 1x 54x 54x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | /**
* DataFlint Error Classes
*
* Provides typed errors for better error handling and user messaging.
* Each error type includes:
* - code: Unique error code for programmatic handling
* - isRetryable: Whether the operation can be retried
* - userAction: Suggested action for the user
*/
// Re-export all error types from auth module (formerly shared-auth-config)
export {
ErrorCode,
DataFlintError,
AuthError,
ConfigError,
NetworkError,
} from "./auth/errors.js";
// Import for local use
import { ErrorCode, DataFlintError } from "./auth/errors.js";
/**
* API-related errors (HTTP response errors)
*/
export class ApiError extends DataFlintError {
readonly code: ErrorCode;
readonly isRetryable: boolean;
readonly userAction: string;
readonly statusCode: number;
readonly retryAfter?: number;
readonly detail?: string;
readonly errorCode?: string;
constructor(
code: ErrorCode,
message: string,
userAction: string,
statusCode: number,
isRetryable: boolean = false,
retryAfter?: number,
cause?: Error,
detail?: string,
errorCode?: string
) {
super(message, cause);
this.code = code;
this.statusCode = statusCode;
this.isRetryable = isRetryable;
this.userAction = userAction;
this.retryAfter = retryAfter;
this.detail = detail;
this.errorCode = errorCode;
}
static unauthorized(
detail?: string,
errorCode?: string,
cause?: Error
): ApiError {
return new ApiError(
ErrorCode.API_UNAUTHORIZED,
"Authentication required or session expired",
'Please run "DataFlint: Login" to authenticate.',
401,
false,
undefined,
cause,
detail,
errorCode
);
}
static accountBlocked(detail?: string, cause?: Error): ApiError {
return new ApiError(
ErrorCode.API_ACCOUNT_BLOCKED,
"Your DataFlint account has been blocked by a workspace administrator",
"Ask a workspace administrator to restore your access. Signing in again will not help while the account is blocked.",
401,
false,
undefined,
cause,
detail,
"user_blocked"
);
}
static forbidden(
detail?: string,
errorCode?: string,
cause?: Error
): ApiError {
return new ApiError(
ErrorCode.API_FORBIDDEN,
"You don't have permission to access this resource",
"Please contact your administrator if you believe this is an error.",
403,
false,
undefined,
cause,
detail,
errorCode
);
}
static notFound(resource: string, cause?: Error): ApiError {
return new ApiError(
ErrorCode.API_NOT_FOUND,
`Resource not found: ${resource}`,
"The requested data may have been deleted or may not exist.",
404,
false,
undefined,
cause
);
}
static rateLimited(retryAfter?: number, cause?: Error): ApiError {
const waitTime = retryAfter
? `Please wait ${retryAfter} seconds`
: "Please wait a moment";
return new ApiError(
ErrorCode.API_RATE_LIMITED,
"Too many requests",
`${waitTime} and try again.`,
429,
true,
retryAfter,
cause
);
}
static serverError(
statusCode: number,
details?: string,
cause?: Error
): ApiError {
return new ApiError(
ErrorCode.API_SERVER_ERROR,
details
? `DataFlint server error: ${details}`
: "DataFlint server encountered an error",
"This is a temporary issue. Please try again later.",
statusCode,
true,
undefined,
cause
);
}
static badRequest(
details?: string,
statusCode: number = 400,
cause?: Error
): ApiError {
return new ApiError(
ErrorCode.API_BAD_REQUEST,
details ? `Invalid request: ${details}` : "Invalid request",
'Run "DataFlint: Show Logs" for details.',
statusCode,
false,
undefined,
cause
);
}
}
/**
* Service-related errors
*/
export class ServiceError extends DataFlintError {
readonly code: ErrorCode;
readonly isRetryable: boolean;
readonly userAction: string;
constructor(
code: ErrorCode,
message: string,
userAction: string,
isRetryable: boolean = false,
cause?: Error
) {
super(message, cause);
this.code = code;
this.isRetryable = isRetryable;
this.userAction = userAction;
}
static notStarted(): ServiceError {
return new ServiceError(
ErrorCode.SERVICE_NOT_STARTED,
"DataFlint service is not running",
'Please run "DataFlint: Login" to start the service.',
false
);
}
static unavailable(cause?: Error): ServiceError {
return new ServiceError(
ErrorCode.SERVICE_UNAVAILABLE,
"DataFlint service is temporarily unavailable",
'Please try "DataFlint: Restart Server" or check your connection.',
true,
cause
);
}
}
// Import error classes for type guards
import { AuthError, ConfigError, NetworkError } from "./auth/errors.js";
/**
* Helper functions to classify errors
*/
export function isAuthError(error: unknown): error is AuthError {
return error instanceof AuthError;
}
export function isConfigError(error: unknown): error is ConfigError {
return error instanceof ConfigError;
}
export function isNetworkError(error: unknown): error is NetworkError {
return error instanceof NetworkError;
}
export function isApiError(error: unknown): error is ApiError {
return error instanceof ApiError;
}
export function isServiceError(error: unknown): error is ServiceError {
return error instanceof ServiceError;
}
export function isDataFlintError(error: unknown): error is DataFlintError {
return error instanceof DataFlintError;
}
// Pattern matchers for error classification - explicit and testable
const ERROR_PATTERNS = {
// Network connection errors (Node.js error codes)
networkUnavailable: ["econnrefused", "econnreset", "fetch failed"],
networkTimeout: ["etimedout", "timeout", "aborted"],
networkDns: ["enotfound", "getaddrinfo"],
// Service state errors
serviceNotStarted: ["not started", "not initialized"],
} as const;
/**
* Map HTTP status code to ApiError
*/
function classifyHttpStatus(statusCode: number, cause: Error): ApiError {
switch (statusCode) {
case 401:
return ApiError.unauthorized(undefined, undefined, cause);
case 403:
return ApiError.forbidden(undefined, undefined, cause);
case 404:
return ApiError.notFound("resource", cause);
case 429:
return ApiError.rateLimited(undefined, cause);
default:
return statusCode >= 500
? ApiError.serverError(statusCode, undefined, cause)
: ApiError.badRequest(undefined, statusCode, cause);
}
}
/**
* Check if message matches any pattern in the list
*/
function matchesPattern(message: string, patterns: readonly string[]): boolean {
return patterns.some((pattern) => message.includes(pattern));
}
/**
* Classify a generic error into a DataFlint error type.
* Uses explicit pattern matching for maintainability.
*/
export function classifyError(error: unknown): DataFlintError {
// Already a DataFlintError - return as-is
if (isDataFlintError(error)) {
return error;
}
const err = error instanceof Error ? error : new Error(String(error));
const message = err.message.toLowerCase();
// 1. Network errors (most common in practice)
if (matchesPattern(message, ERROR_PATTERNS.networkUnavailable)) {
return NetworkError.unavailable(err);
}
if (matchesPattern(message, ERROR_PATTERNS.networkTimeout)) {
return NetworkError.timeout(err);
}
if (matchesPattern(message, ERROR_PATTERNS.networkDns)) {
return NetworkError.dnsFailed("unknown", err);
}
// 2. HTTP status codes (from fetch responses)
const httpMatch = message.match(/\b(\d{3})\b/);
if (httpMatch) {
const statusCode = parseInt(httpMatch[1], 10);
if (statusCode >= 400 && statusCode < 600) {
return classifyHttpStatus(statusCode, err);
}
}
// 3. Service state errors
if (matchesPattern(message, ERROR_PATTERNS.serviceNotStarted)) {
return ServiceError.notStarted();
}
// 4. Default: unknown error
return new ServiceError(
ErrorCode.UNKNOWN,
err.message || "An unexpected error occurred",
'Run "DataFlint: Show Logs" for more details.',
false,
err
);
}
|