@ai-growth/n8n-nodes-wordpress
Version:
n8n node for WordPress integration with AI GROWTH - SEO WP plugin
144 lines (143 loc) • 6.11 kB
JavaScript
;
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
if (typeof b !== "function" && b !== null)
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
exports.__esModule = true;
exports.ErrorUtils = exports.WordPressError = exports.WordPressErrorType = void 0;
var axios_1 = require("axios");
/**
* Tipo de erro da API WordPress
*/
var WordPressErrorType;
(function (WordPressErrorType) {
WordPressErrorType["AUTHENTICATION"] = "authentication";
WordPressErrorType["PERMISSION"] = "permission";
WordPressErrorType["NOT_FOUND"] = "not_found";
WordPressErrorType["VALIDATION"] = "validation";
WordPressErrorType["VALIDATION_FAILED"] = "validation_failed";
WordPressErrorType["SERVER"] = "server";
WordPressErrorType["NETWORK"] = "network";
WordPressErrorType["TIMEOUT"] = "timeout";
WordPressErrorType["UNKNOWN"] = "unknown";
})(WordPressErrorType = exports.WordPressErrorType || (exports.WordPressErrorType = {}));
/**
* Erro específico da API WordPress
*/
var WordPressError = /** @class */ (function (_super) {
__extends(WordPressError, _super);
function WordPressError(message, type, statusCode, originalError) {
if (type === void 0) { type = WordPressErrorType.UNKNOWN; }
var _this = _super.call(this, message) || this;
_this.name = 'WordPressError';
_this.type = type;
_this.statusCode = statusCode;
_this.originalError = originalError;
return _this;
}
/**
* Verifica se o erro é retentável
* @returns Verdadeiro se o erro pode ser tentado novamente
*/
WordPressError.prototype.isRetryable = function () {
// Erros de rede e servidor geralmente são retentáveis
return this.type === WordPressErrorType.NETWORK
|| this.type === WordPressErrorType.SERVER
|| this.type === WordPressErrorType.TIMEOUT;
};
/**
* Cria uma representação legível do erro
* @returns Objeto com informações sobre o erro
*/
WordPressError.prototype.toJSON = function () {
return {
name: this.name,
message: this.message,
type: this.type,
statusCode: this.statusCode,
stack: this.stack
};
};
return WordPressError;
}(Error));
exports.WordPressError = WordPressError;
/**
* Classe utilitária para lidar com erros
*/
var ErrorUtils = /** @class */ (function () {
function ErrorUtils() {
}
/**
* Converte um erro do Axios em um WordPressError
* @param error Erro do Axios
* @returns Erro da API WordPress
*/
ErrorUtils.fromAxiosError = function (error) {
if (error.response) {
// Erro com resposta do servidor
var status_1 = error.response.status;
// Extrair mensagem de erro da resposta, se disponível
var errorMessage = 'WordPress API error';
if (error.response.data && typeof error.response.data === 'object') {
if ('message' in error.response.data) {
errorMessage = error.response.data.message || errorMessage;
}
else if ('error' in error.response.data) {
errorMessage = error.response.data.error || errorMessage;
}
}
if (status_1 === 401) {
return new WordPressError('Authentication failed: Invalid username or password', WordPressErrorType.AUTHENTICATION, status_1, error);
}
else if (status_1 === 403) {
return new WordPressError('Permission denied: Insufficient privileges', WordPressErrorType.PERMISSION, status_1, error);
}
else if (status_1 === 404) {
return new WordPressError('Resource not found', WordPressErrorType.NOT_FOUND, status_1, error);
}
else if (status_1 >= 400 && status_1 < 500) {
return new WordPressError("Client error: ".concat(errorMessage), WordPressErrorType.VALIDATION, status_1, error);
}
else if (status_1 >= 500) {
return new WordPressError("Server error: ".concat(errorMessage), WordPressErrorType.SERVER, status_1, error);
}
}
else if (error.request) {
// Requisição feita mas sem resposta
if (error.code === 'ECONNABORTED') {
return new WordPressError('Request timeout: The server took too long to respond', WordPressErrorType.TIMEOUT, undefined, error);
}
return new WordPressError('Network error: Failed to connect to the WordPress server', WordPressErrorType.NETWORK, undefined, error);
}
// Erro genérico
return new WordPressError(error.message || 'Unknown error occurred', WordPressErrorType.UNKNOWN, undefined, error);
};
/**
* Converte um erro genérico em um WordPressError
* @param error Erro genérico
* @returns Erro da API WordPress
*/
ErrorUtils.fromError = function (error) {
if (error instanceof WordPressError) {
return error;
}
// Verificar se é um erro do Axios
if (axios_1["default"].isAxiosError(error)) {
return this.fromAxiosError(error);
}
return new WordPressError(error.message || 'Unknown error occurred', WordPressErrorType.UNKNOWN, undefined, error);
};
return ErrorUtils;
}());
exports.ErrorUtils = ErrorUtils;