This is an automated email from the ASF dual-hosted git repository.

likyh pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git


The following commit(s) were added to refs/heads/main by this push:
     new 76507b4e3 docs: improve swagger doc and error message (#4589)
76507b4e3 is described below

commit 76507b4e3b6e5f59ff2e8340fc8c7561f9279b71
Author: mindlesscloud <[email protected]>
AuthorDate: Mon Mar 6 15:36:44 2023 +0800

    docs: improve swagger doc and error message (#4589)
---
 backend/plugins/customize/api/field.go       | 46 ++++++++++++++++------------
 backend/plugins/customize/service/service.go |  6 ++--
 2 files changed, 30 insertions(+), 22 deletions(-)

diff --git a/backend/plugins/customize/api/field.go 
b/backend/plugins/customize/api/field.go
index 0d2d8b993..23b0149ab 100644
--- a/backend/plugins/customize/api/field.go
+++ b/backend/plugins/customize/api/field.go
@@ -30,15 +30,19 @@ import (
        "github.com/apache/incubator-devlake/plugins/customize/service"
 )
 
-type field struct {
-       ColumnName        string `json:"columnName" example:"x_column_varchar"`
-       DisplayName       string `json:"displayName" example:"department"`
-       DataType          string `json:"dataType" example:"varchar(255)"`
-       Description       string `json:"description" example:"more details 
about the column"`
-       IsCustomizedField bool   `json:"isCustomizedField" example:"true"`
+type fieldResponse struct {
+       Field
+       IsCustomizedField bool `json:"isCustomizedField" example:"true"`
 }
 
-func (f *field) toCustomizedField(table string) (*models.CustomizedField, 
errors.Error) {
+type Field struct {
+       ColumnName  string `json:"columnName" example:"x_column_varchar"`
+       DisplayName string `json:"displayName" example:"department"`
+       DataType    string `json:"dataType" example:"varchar(255)"`
+       Description string `json:"description" example:"more details about the 
column"`
+}
+
+func (f *Field) toDBModel(table string) (*models.CustomizedField, 
errors.Error) {
        if !strings.HasPrefix(f.ColumnName, "x_") {
                return nil, errors.BadInput.New("the columnName should start 
with x_")
        }
@@ -58,12 +62,14 @@ func (f *field) toCustomizedField(table string) 
(*models.CustomizedField, errors
        }, nil
 }
 
-func fromCustomizedField(cf models.CustomizedField) field {
-       return field{
-               ColumnName:        cf.ColumnName,
-               DisplayName:       cf.DisplayName,
-               DataType:          cf.DataType.String(),
-               Description:       cf.Description,
+func fromCustomizedField(cf models.CustomizedField) fieldResponse {
+       return fieldResponse{
+               Field: Field{
+                       ColumnName:  cf.ColumnName,
+                       DisplayName: cf.DisplayName,
+                       DataType:    cf.DataType.String(),
+                       Description: cf.Description,
+               },
                IsCustomizedField: strings.HasPrefix(cf.ColumnName, "x_"),
        }
 }
@@ -81,7 +87,7 @@ func NewHandlers(dal dal.Dal) *Handlers {
 // @Description return all customized fieldsh
 // @Tags plugins/customize
 // @Param table path string true "the table name"
-// @Success 200  {object} []field "Success"
+// @Success 200  {object} []fieldResponse "Success"
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/customize/{table}/fields [GET]
@@ -90,7 +96,7 @@ func (h *Handlers) ListFields(input *plugin.ApiResourceInput) 
(*plugin.ApiResour
        if err != nil {
                return &plugin.ApiResourceOutput{Status: 
http.StatusBadRequest}, errors.Default.Wrap(err, "getFields error")
        }
-       fields := make([]field, 0, len(customizedFields))
+       fields := make([]fieldResponse, 0, len(customizedFields))
        for _, cf := range customizedFields {
                fields = append(fields, fromCustomizedField(cf))
        }
@@ -102,19 +108,19 @@ func (h *Handlers) ListFields(input 
*plugin.ApiResourceInput) (*plugin.ApiResour
 // @Description create a customized field
 // @Tags plugins/customize
 // @Param table path string true "the table name"
-// @Param request body field true "request body"
-// @Success 200  {object} field "Success"
+// @Param request body Field true "request body"
+// @Success 200  {object} fieldResponse "Success"
 // @Failure 400  {object} shared.ApiBody "Bad Request"
 // @Failure 500  {object} shared.ApiBody "Internal Error"
 // @Router /plugins/customize/{table}/fields [POST]
 func (h *Handlers) CreateFields(input *plugin.ApiResourceInput) 
(*plugin.ApiResourceOutput, errors.Error) {
        table := input.Params["table"]
-       fld := &field{}
+       fld := &Field{}
        err := helper.Decode(input.Body, fld, nil)
        if err != nil {
                return &plugin.ApiResourceOutput{Status: 
http.StatusBadRequest}, err
        }
-       customizedField, err := fld.toCustomizedField(table)
+       customizedField, err := fld.toDBModel(table)
        if err != nil {
                return &plugin.ApiResourceOutput{Status: 
http.StatusBadRequest}, err
        }
@@ -122,7 +128,7 @@ func (h *Handlers) CreateFields(input 
*plugin.ApiResourceInput) (*plugin.ApiReso
        if err != nil {
                return nil, errors.Default.Wrap(err, "CreateField error")
        }
-       return &plugin.ApiResourceOutput{Body: fld, Status: http.StatusOK}, nil
+       return &plugin.ApiResourceOutput{Body: fieldResponse{*fld, true}, 
Status: http.StatusOK}, nil
 }
 
 // DeleteField delete a customized fields
diff --git a/backend/plugins/customize/service/service.go 
b/backend/plugins/customize/service/service.go
index ec2fe892a..416e52789 100644
--- a/backend/plugins/customize/service/service.go
+++ b/backend/plugins/customize/service/service.go
@@ -184,10 +184,12 @@ func (s *Service) importCSV(file io.ReadCloser, 
rawDataParams string, recordHand
                return err
        }
        var hasNext bool
+       var line int
        now := time.Now()
        for {
+               line++
                if hasNext, err = iterator.HasNextWithError(); !hasNext {
-                       return err
+                       return errors.BadInput.Wrap(err, fmt.Sprintf("error on 
processing the line:%d", line))
                } else {
                        record := iterator.Fetch()
                        record["_raw_data_params"] = rawDataParams
@@ -200,7 +202,7 @@ func (s *Service) importCSV(file io.ReadCloser, 
rawDataParams string, recordHand
                        record["updated_at"] = now
                        err = recordHandler(record)
                        if err != nil {
-                               return err
+                               return errors.BadInput.Wrap(err, 
fmt.Sprintf("error on processing the line:%d", line))
                        }
                }
        }

Reply via email to