add more error hints

This commit is contained in:
MaysWind
2024-09-11 00:47:26 +08:00
parent 5d75629a73
commit 031209490f
6 changed files with 56 additions and 15 deletions
@@ -199,7 +199,7 @@ func (c *DataTableTransactionDataExporter) replaceDelimiters(text string) string
func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, user *models.User, dataTable ImportedDataTable, defaultTimezoneOffset int16, accountMap map[string]*models.Account, categoryMap map[string]*models.TransactionCategory, tagMap map[string]*models.TransactionTag) (models.ImportedTransactionSlice, []*models.Account, []*models.TransactionCategory, []*models.TransactionTag, error) {
if dataTable.DataRowCount() < 1 {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse import data for user \"uid:%d\", because data table row count is less 1", user.Uid)
return nil, nil, nil, nil, errs.ErrOperationFailed
return nil, nil, nil, nil, errs.ErrNotFoundTransactionDataInFile
}
headerLineItems := dataTable.HeaderLineColumnNames()
@@ -226,7 +226,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if !timeColumnExists || !typeColumnExists || !subCategoryColumnExists ||
!accountColumnExists || !amountColumnExists || !account2ColumnExists || !amount2ColumnExists {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse import data for user \"uid:%d\", because missing essential columns in header row", user.Uid)
return nil, nil, nil, nil, errs.ErrFormatInvalid
return nil, nil, nil, nil, errs.ErrMissingRequiredFieldInHeaderRow
}
if accountMap == nil {
@@ -260,7 +260,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if columnCount < len(headerLineItems) {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse data row \"index:%d\" for user \"uid:%d\", because may missing some columns (column count %d in data row is less than header column count %d)", dataRowIndex, user.Uid, columnCount, len(headerLineItems))
return nil, nil, nil, nil, errs.ErrFormatInvalid
return nil, nil, nil, nil, errs.ErrFewerFieldsInDataRowThanInHeaderRow
}
timezoneOffset := defaultTimezoneOffset
@@ -270,7 +270,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse time zone \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(timezoneColumnIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrTransactionTimeZoneInvalid
}
timezoneOffset = utils.GetTimezoneOffsetMinutes(transactionTimezone)
@@ -280,14 +280,14 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse time \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(timeColumnIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrTransactionTimeInvalid
}
transactionDbType, err := c.getTransactionDbType(dataRow.GetData(typeColumnIdx))
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse transaction type \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(typeColumnIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.Or(err, errs.ErrTransactionTypeInvalid)
}
categoryId := int64(0)
@@ -298,14 +298,14 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse transaction category type in data row \"index:%d\" for user \"uid:%d\", because %s", dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.Or(err, errs.ErrTransactionTypeInvalid)
}
subCategoryName = dataRow.GetData(subCategoryColumnIdx)
if subCategoryName == "" {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] sub category type is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid)
return nil, nil, nil, nil, errs.ErrFormatInvalid
return nil, nil, nil, nil, errs.ErrSubCategoryNameCannotBeBlank
}
subCategory, exists := categoryMap[subCategoryName]
@@ -323,7 +323,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if accountName == "" {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account name is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid)
return nil, nil, nil, nil, errs.ErrFormatInvalid
return nil, nil, nil, nil, errs.ErrAccountNameCannotBeBlank
}
accountCurrency := user.DefaultCurrency
@@ -358,7 +358,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse acmount \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(amountColumnIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrAmountInvalid
}
relatedAccountId := int64(0)
@@ -371,7 +371,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if account2Name == "" {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] account2 name is empty in data row \"index:%d\" for user \"uid:%d\"", dataRowIndex, user.Uid)
return nil, nil, nil, nil, errs.ErrFormatInvalid
return nil, nil, nil, nil, errs.ErrDestinationAccountNameCannotBeBlank
}
account2Currency = user.DefaultCurrency
@@ -407,7 +407,7 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse acmount2 \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(amount2ColumnIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrAmountInvalid
}
}
@@ -422,14 +422,14 @@ func (c *DataTableTransactionDataImporter) parseImportedData(ctx core.Context, u
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse geographic location \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(geoLocationIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrGeographicLocationInvalid
}
geoLatitude, err = utils.StringToFloat64(geoLocationItems[1])
if err != nil {
log.Errorf(ctx, "[data_table_transaction_data_converter.parseImportedData] cannot parse geographic location \"%s\" in data row \"index:%d\" for user \"uid:%d\", because %s", dataRow.GetData(geoLocationIdx), dataRowIndex, user.Uid, err.Error())
return nil, nil, nil, nil, err
return nil, nil, nil, nil, errs.ErrGeographicLocationInvalid
}
}
}
@@ -144,7 +144,7 @@ func createNewezbookkeepingTransactionPlainTextDataTable(content string, columnS
allLines := strings.Split(content, lineSeparator)
if len(allLines) < 2 {
return nil, errs.ErrOperationFailed
return nil, errs.ErrNotFoundTransactionDataInFile
}
headerLine := allLines[0]
+18
View File
@@ -0,0 +1,18 @@
package errs
import "net/http"
// Error codes related to data converters
var (
ErrNotFoundTransactionDataInFile = NewNormalError(NormalSubcategoryConverter, 0, http.StatusBadRequest, "not found transaction data")
ErrMissingRequiredFieldInHeaderRow = NewNormalError(NormalSubcategoryConverter, 1, http.StatusBadRequest, "missing required field in header row")
ErrFewerFieldsInDataRowThanInHeaderRow = NewNormalError(NormalSubcategoryConverter, 2, http.StatusBadRequest, "fewer fields in data row than in header row")
ErrTransactionTimeInvalid = NewNormalError(NormalSubcategoryConverter, 3, http.StatusBadRequest, "transaction time is invalid")
ErrTransactionTimeZoneInvalid = NewNormalError(NormalSubcategoryConverter, 4, http.StatusBadRequest, "transaction time zone is invalid")
ErrCategoryNameCannotBeBlank = NewNormalError(NormalSubcategoryConverter, 5, http.StatusBadRequest, "category name cannot be blank")
ErrSubCategoryNameCannotBeBlank = NewNormalError(NormalSubcategoryConverter, 6, http.StatusBadRequest, "secondary category name cannot be blank")
ErrAccountNameCannotBeBlank = NewNormalError(NormalSubcategoryConverter, 7, http.StatusBadRequest, "account name cannot be blank")
ErrDestinationAccountNameCannotBeBlank = NewNormalError(NormalSubcategoryConverter, 8, http.StatusBadRequest, "destination account name cannot be blank")
ErrAmountInvalid = NewNormalError(NormalSubcategoryConverter, 9, http.StatusBadRequest, "transaction amount is invalid")
ErrGeographicLocationInvalid = NewNormalError(NormalSubcategoryConverter, 10, http.StatusBadRequest, "geographic location is invalid")
)
+1
View File
@@ -37,6 +37,7 @@ const (
NormalSubcategoryMapProxy = 9
NormalSubcategoryTemplate = 10
NormalSubcategoryPicture = 11
NormalSubcategoryConverter = 12
)
// Error represents the specific error returned to user
+11
View File
@@ -1106,6 +1106,17 @@
"transaction picture not exists": "Transaction picture does not exist",
"transaction picture file extension invalid": "Transaction picture file extension is invalid",
"exceed the maximum size of transaction picture file": "The uploaded transaction picture exceeds the maximum allowed file size",
"not found transaction data": "No transaction data found in file",
"missing required field in header row": "Missing required field in header row",
"fewer fields in data row than in header row": "There are fewer fields in the data row than in the header row",
"transaction time is invalid": "Transaction time is invalid",
"transaction time zone is invalid": "Transaction time zone is invalid",
"category name cannot be blank": "Category name cannot be blank",
"secondary category name cannot be blank": "secondary category name cannot be blank",
"account name cannot be blank": "Account name cannot be blank",
"destination account name cannot be blank": "Destination account name cannot be blank",
"transaction amount is invalid": "Transaction amount is invalid",
"geographic location is invalid": "Geographic location is invalid",
"query items cannot be blank": "There are no query items",
"query items too much": "There are too many query items",
"query items have invalid item": "There is invalid item in query items",
+11
View File
@@ -1106,6 +1106,17 @@
"transaction picture not exists": "交易图片不存在",
"transaction picture file extension invalid": "交易图片文件扩展名无效",
"exceed the maximum size of transaction picture file": "上传的交易图片超出了允许的最大文件大小",
"not found transaction data": "文件中没有找到交易数据",
"missing required field in header row": "标题行中缺少必要的字段",
"fewer fields in data row than in header row": "数据行中的字段少于比标题行中的字段",
"transaction time is invalid": "交易时间无效",
"transaction time zone is invalid": "交易时区无效",
"category name cannot be blank": "分类名称不能为空",
"secondary category name cannot be blank": "二级分类名称不能为空",
"account name cannot be blank": "账户名不能为空",
"destination account name cannot be blank": "目标账户名不能为空",
"transaction amount is invalid": "交易金额无效",
"geographic location is invalid": "地理位置无效",
"query items cannot be blank": "请求项目不能为空",
"query items too much": "请求项目过多",
"query items have invalid item": "请求项目中有非法项目",