
//#region src/api-client/errors.ts
/**
* API Error Class Hierarchy
*
* This module defines a hierarchical error system for the Sentry API client
* that automatically categorizes errors based on HTTP status codes.
*
* Key principles:
* - 4xx errors (ApiClientError) are user input errors, not reported to Sentry
* - 5xx errors (ApiServerError) are system errors, reported to Sentry
* - Specific error types provide additional context and helper methods
* - The hierarchy enables type-safe error handling without manual status checks
*/
/**
* Base class for all API errors.
* Contains common properties for all API error types.
* Status is optional since some errors (like client-side validation) don't have HTTP status codes.
*/
var ApiError = class ApiError extends Error {
	constructor(message, status, detail, responseBody) {
		super(message);
		this.status = status;
		this.detail = detail;
		this.responseBody = responseBody;
		this.name = "ApiError";
		Object.setPrototypeOf(this, ApiError.prototype);
	}
};
/**
* Client errors (4xx) - User input errors that should NOT be reported to Sentry.
* These typically indicate issues with the request that the user can fix.
* Also includes client-side validation errors that don't have HTTP status codes.
*/
var ApiClientError = class ApiClientError extends ApiError {
	constructor(message, status, detail, responseBody) {
		super(message, status, detail, responseBody);
		this.name = "ApiClientError";
		Object.setPrototypeOf(this, ApiClientError.prototype);
	}
	/**
	* Convert to a user-friendly formatted message.
	* Returns the standard format: "API error (status): message" or just "API error: message" if no status.
	* For 404 errors, adds helpful context about checking parameters.
	*/
	toUserMessage() {
		const baseMessage = this.status ? `API error (${this.status}): ${this.message}` : `API error: ${this.message}`;
		if (this.status === 404) {
			const genericMessages = [
				"not found",
				"the requested resource does not exist",
				"resource does not exist",
				"resource not found"
			];
			const isGeneric = genericMessages.some((msg) => this.message.toLowerCase() === msg || this.message.toLowerCase().includes("requested resource"));
			if (isGeneric) return `${baseMessage}. Please verify that the organization, project, or resource ID is correct and that you have access to it.`;
			return `${baseMessage}. Please verify the parameters are correct.`;
		}
		return baseMessage;
	}
	/**
	* Check if this is a permission/authorization error (403)
	*/
	isPermissionError() {
		return this.status === 403;
	}
	/**
	* Check if this is a not found error (404)
	*/
	isNotFoundError() {
		return this.status === 404;
	}
	/**
	* Check if this is a validation error (400 or 422)
	*/
	isValidationError() {
		return this.status === 400 || this.status === 422;
	}
	/**
	* Check if this is an authentication error (401)
	*/
	isAuthenticationError() {
		return this.status === 401;
	}
	/**
	* Check if this is a rate limit error (429)
	*/
	isRateLimitError() {
		return this.status === 429;
	}
};
/**
* Permission denied error (403).
* Includes special handling for multi-project access errors.
*/
var ApiPermissionError = class ApiPermissionError extends ApiClientError {
	constructor(message, detail, responseBody) {
		super(message, 403, detail, responseBody);
		this.name = "ApiPermissionError";
		Object.setPrototypeOf(this, ApiPermissionError.prototype);
	}
	/**
	* Check if this is the specific multi-project access error
	*/
	isMultiProjectAccessError() {
		return this.message.includes("multiple projects") || this.message.includes("multi project") || this.message.includes("multi-project") || this.message.includes("You do not have access to query across multiple projects");
	}
};
/**
* Resource not found error (404).
* Can include additional context about what resource was not found.
*/
var ApiNotFoundError = class ApiNotFoundError extends ApiClientError {
	constructor(message, detail, responseBody, resourceType, resourceId) {
		super(message, 404, detail, responseBody);
		this.resourceType = resourceType;
		this.resourceId = resourceId;
		this.name = "ApiNotFoundError";
		Object.setPrototypeOf(this, ApiNotFoundError.prototype);
	}
};
/**
* Validation error (400 or 422 for API responses, or no status for client-side validation).
* Indicates the request was malformed or contained invalid data.
*/
var ApiValidationError = class ApiValidationError extends ApiClientError {
	constructor(message, status, detail, responseBody, validationErrors) {
		super(message, status, detail, responseBody);
		this.validationErrors = validationErrors;
		this.name = "ApiValidationError";
		Object.setPrototypeOf(this, ApiValidationError.prototype);
	}
};
/**
* Authentication error (401).
* Indicates the request lacks valid authentication credentials.
*/
var ApiAuthenticationError = class ApiAuthenticationError extends ApiClientError {
	constructor(message, detail, responseBody) {
		super(message, 401, detail, responseBody);
		this.name = "ApiAuthenticationError";
		Object.setPrototypeOf(this, ApiAuthenticationError.prototype);
	}
};
/**
* Rate limit error (429).
* Includes retry-after information when available.
*/
var ApiRateLimitError = class ApiRateLimitError extends ApiClientError {
	constructor(message, detail, responseBody, retryAfter) {
		super(message, 429, detail, responseBody);
		this.retryAfter = retryAfter;
		this.name = "ApiRateLimitError";
		Object.setPrototypeOf(this, ApiRateLimitError.prototype);
	}
};
/**
* Server errors (5xx) - System errors that SHOULD be reported to Sentry.
* These indicate problems with the server that are not the user's fault.
*/
var ApiServerError = class ApiServerError extends ApiError {
	constructor(message, status, detail, responseBody) {
		super(message, status, detail, responseBody);
		this.name = "ApiServerError";
		Object.setPrototypeOf(this, ApiServerError.prototype);
	}
	/**
	* Check if this is a gateway/proxy error (502, 503, 504)
	*/
	isGatewayError() {
		return this.status === 502 || this.status === 503 || this.status === 504;
	}
	/**
	* Check if this is an internal server error (500)
	*/
	isInternalError() {
		return this.status === 500;
	}
};
/**
* Factory function to create the appropriate error type based on HTTP status code.
* This centralizes the logic for determining which error class to instantiate.
* This is only used for actual API responses, so status is required.
*/
function createApiError(message, status, detail, responseBody) {
	let improvedMessage = message;
	if (message.includes("You do not have the multi project stream feature enabled") || message.includes("You cannot view events from multiple projects")) {
		improvedMessage = "You do not have access to query across multiple projects. Please select a project for your query.";
		return new ApiPermissionError(improvedMessage, detail, responseBody);
	}
	switch (status) {
		case 401: return new ApiAuthenticationError(message, detail, responseBody);
		case 403: return new ApiPermissionError(message, detail, responseBody);
		case 404: return new ApiNotFoundError(message, detail, responseBody);
		case 400:
		case 422: {
			let validationErrors;
			if (responseBody && typeof responseBody === "object" && "errors" in responseBody) validationErrors = responseBody.errors;
			return new ApiValidationError(message, status, detail, responseBody, validationErrors);
		}
		case 429: {
			let retryAfter;
			if (responseBody && typeof responseBody === "object") {
				if ("retry_after" in responseBody) retryAfter = Number(responseBody.retry_after);
				else if ("retryAfter" in responseBody) retryAfter = Number(responseBody.retryAfter);
			}
			return new ApiRateLimitError(message, detail, responseBody, retryAfter);
		}
		default:
			if (status >= 400 && status < 500) return new ApiClientError(message, status, detail, responseBody);
			if (status >= 500 && status < 600) return new ApiServerError(message, status, detail, responseBody);
			return new ApiError(message, status, detail, responseBody);
	}
}

//#endregion
Object.defineProperty(exports, 'ApiAuthenticationError', {
  enumerable: true,
  get: function () {
    return ApiAuthenticationError;
  }
});
Object.defineProperty(exports, 'ApiClientError', {
  enumerable: true,
  get: function () {
    return ApiClientError;
  }
});
Object.defineProperty(exports, 'ApiError', {
  enumerable: true,
  get: function () {
    return ApiError;
  }
});
Object.defineProperty(exports, 'ApiNotFoundError', {
  enumerable: true,
  get: function () {
    return ApiNotFoundError;
  }
});
Object.defineProperty(exports, 'ApiPermissionError', {
  enumerable: true,
  get: function () {
    return ApiPermissionError;
  }
});
Object.defineProperty(exports, 'ApiRateLimitError', {
  enumerable: true,
  get: function () {
    return ApiRateLimitError;
  }
});
Object.defineProperty(exports, 'ApiServerError', {
  enumerable: true,
  get: function () {
    return ApiServerError;
  }
});
Object.defineProperty(exports, 'ApiValidationError', {
  enumerable: true,
  get: function () {
    return ApiValidationError;
  }
});
Object.defineProperty(exports, 'createApiError', {
  enumerable: true,
  get: function () {
    return createApiError;
  }
});
//# sourceMappingURL=errors-Bmqdg5Q6.cjs.map