☐ Implement LINE Mini dApp with gamified USDT yield farming
☐ Deploy smart contracts to Kaia mainnet with real USDT
☐ Create comprehensive Dune Analytics dashboard
☐ Build AI-powered chat bot for LINE integration
☐ Implement DID-based credentials and social features
☐ Create demo video and pitch deck
☐ Deploy live production environment
⏺ 🚀 WINNING STRATEGY: KAIA YIELD AI
"AI-Powered Stablecoin Yield Optimization with Gamified LINE Mini dApp"
Let me build the winning features that will set us apart:
Unique Value Proposition:
1. AI-Driven Yield Optimization - Machine learning for optimal USDT allocation
2. LINE Messenger Integration - Native social trading experience
3. Gamified Missions - Social earning mechanics in LINE
4. DID-Based Credentials - LINE ID linked reputation system
5. Real-Time Analytics - Comprehensive Dune dashboard
211 lines
5.8 KiB
JavaScript
211 lines
5.8 KiB
JavaScript
const winston = require('winston');
|
|
|
|
const logger = winston.createLogger({
|
|
level: 'error',
|
|
format: winston.format.combine(
|
|
winston.format.timestamp(),
|
|
winston.format.errors({ stack: true }),
|
|
winston.format.json()
|
|
),
|
|
transports: [
|
|
new winston.transports.File({ filename: 'logs/error.log' }),
|
|
new winston.transports.Console({
|
|
format: winston.format.simple()
|
|
})
|
|
]
|
|
});
|
|
|
|
class AppError extends Error {
|
|
constructor(message, statusCode, code = null, details = null) {
|
|
super(message);
|
|
this.statusCode = statusCode;
|
|
this.status = `${statusCode}`.startsWith('4') ? 'fail' : 'error';
|
|
this.isOperational = true;
|
|
this.code = code;
|
|
this.details = details;
|
|
|
|
Error.captureStackTrace(this, this.constructor);
|
|
}
|
|
}
|
|
|
|
class ValidationError extends AppError {
|
|
constructor(message, details = null) {
|
|
super(message, 400, 'VALIDATION_ERROR', details);
|
|
}
|
|
}
|
|
|
|
class BlockchainError extends AppError {
|
|
constructor(message, details = null) {
|
|
super(message, 502, 'BLOCKCHAIN_ERROR', details);
|
|
}
|
|
}
|
|
|
|
class DatabaseError extends AppError {
|
|
constructor(message, details = null) {
|
|
super(message, 500, 'DATABASE_ERROR', details);
|
|
}
|
|
}
|
|
|
|
class AuthenticationError extends AppError {
|
|
constructor(message = 'Authentication failed', details = null) {
|
|
super(message, 401, 'AUTH_ERROR', details);
|
|
}
|
|
}
|
|
|
|
class RateLimitError extends AppError {
|
|
constructor(message = 'Rate limit exceeded', details = null) {
|
|
super(message, 429, 'RATE_LIMIT_ERROR', details);
|
|
}
|
|
}
|
|
|
|
const handleCastErrorDB = (err) => {
|
|
const message = `Invalid ${err.path}: ${err.value}`;
|
|
return new ValidationError(message);
|
|
};
|
|
|
|
const handleDuplicateFieldsDB = (err) => {
|
|
const value = err.errmsg.match(/(["'])(\\?.)*?\1/)[0];
|
|
const message = `Duplicate field value: ${value}. Please use another value!`;
|
|
return new ValidationError(message);
|
|
};
|
|
|
|
const handleValidationErrorDB = (err) => {
|
|
const errors = Object.values(err.errors).map(el => el.message);
|
|
const message = `Invalid input data. ${errors.join('. ')}`;
|
|
return new ValidationError(message, errors);
|
|
};
|
|
|
|
const handleJWTError = () =>
|
|
new AuthenticationError('Invalid token. Please log in again!');
|
|
|
|
const handleJWTExpiredError = () =>
|
|
new AuthenticationError('Your token has expired! Please log in again.');
|
|
|
|
const handleBlockchainConnectionError = (err) => {
|
|
const message = 'Blockchain connection failed. Please try again later.';
|
|
return new BlockchainError(message, { originalError: err.message });
|
|
};
|
|
|
|
const handleContractError = (err) => {
|
|
let message = 'Smart contract interaction failed';
|
|
|
|
if (err.message.includes('insufficient funds')) {
|
|
message = 'Insufficient funds for transaction';
|
|
} else if (err.message.includes('gas')) {
|
|
message = 'Transaction failed due to gas issues';
|
|
} else if (err.message.includes('revert')) {
|
|
message = 'Transaction was reverted by smart contract';
|
|
}
|
|
|
|
return new BlockchainError(message, {
|
|
originalError: err.message,
|
|
transactionHash: err.transactionHash
|
|
});
|
|
};
|
|
|
|
const sendErrorDev = (err, req, res) => {
|
|
// API
|
|
if (req.originalUrl.startsWith('/api')) {
|
|
return res.status(err.statusCode).json({
|
|
success: false,
|
|
error: err,
|
|
message: err.message,
|
|
stack: err.stack,
|
|
timestamp: new Date().toISOString(),
|
|
path: req.originalUrl,
|
|
method: req.method
|
|
});
|
|
}
|
|
|
|
// RENDERED WEBSITE
|
|
console.error('ERROR 💥', err);
|
|
return res.status(err.statusCode).render('error', {
|
|
title: 'Something went wrong!',
|
|
msg: err.message
|
|
});
|
|
};
|
|
|
|
const sendErrorProd = (err, req, res) => {
|
|
// API
|
|
if (req.originalUrl.startsWith('/api')) {
|
|
// Operational, trusted error: send message to client
|
|
if (err.isOperational) {
|
|
return res.status(err.statusCode).json({
|
|
success: false,
|
|
error: {
|
|
code: err.code,
|
|
message: err.message,
|
|
...(err.details && { details: err.details })
|
|
},
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
}
|
|
|
|
// Programming or other unknown error: don't leak error details
|
|
logger.error('ERROR 💥', err);
|
|
return res.status(500).json({
|
|
success: false,
|
|
error: {
|
|
code: 'INTERNAL_ERROR',
|
|
message: 'Something went wrong!'
|
|
},
|
|
timestamp: new Date().toISOString()
|
|
});
|
|
}
|
|
|
|
// RENDERED WEBSITE
|
|
if (err.isOperational) {
|
|
return res.status(err.statusCode).render('error', {
|
|
title: 'Something went wrong!',
|
|
msg: err.message
|
|
});
|
|
}
|
|
|
|
// Programming or other unknown error: don't leak error details
|
|
logger.error('ERROR 💥', err);
|
|
return res.status(err.statusCode).render('error', {
|
|
title: 'Something went wrong!',
|
|
msg: 'Please try again later.'
|
|
});
|
|
};
|
|
|
|
const globalErrorHandler = (err, req, res, next) => {
|
|
err.statusCode = err.statusCode || 500;
|
|
err.status = err.status || 'error';
|
|
|
|
if (process.env.NODE_ENV === 'development') {
|
|
sendErrorDev(err, req, res);
|
|
} else {
|
|
let error = { ...err };
|
|
error.message = err.message;
|
|
|
|
// Handle specific error types
|
|
if (error.name === 'CastError') error = handleCastErrorDB(error);
|
|
if (error.code === 11000) error = handleDuplicateFieldsDB(error);
|
|
if (error.name === 'ValidationError') error = handleValidationErrorDB(error);
|
|
if (error.name === 'JsonWebTokenError') error = handleJWTError();
|
|
if (error.name === 'TokenExpiredError') error = handleJWTExpiredError();
|
|
if (error.name === 'NetworkError') error = handleBlockchainConnectionError(error);
|
|
if (error.code === 'CALL_EXCEPTION') error = handleContractError(error);
|
|
|
|
sendErrorProd(error, req, res);
|
|
}
|
|
};
|
|
|
|
const catchAsync = (fn) => {
|
|
return (req, res, next) => {
|
|
Promise.resolve(fn(req, res, next)).catch(next);
|
|
};
|
|
};
|
|
|
|
module.exports = {
|
|
AppError,
|
|
ValidationError,
|
|
BlockchainError,
|
|
DatabaseError,
|
|
AuthenticationError,
|
|
RateLimitError,
|
|
globalErrorHandler,
|
|
catchAsync,
|
|
logger
|
|
}; |