Skip to content

Commit

Permalink
feat: return error in qiniu.New()
Browse files Browse the repository at this point in the history
  • Loading branch information
hsluoyz committed Mar 3, 2024
1 parent 92428b5 commit 5cbbf3b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
8 changes: 4 additions & 4 deletions qiniu/qiniu.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var zonedata = map[string]*storage.Zone{
"beimei": &storage.ZoneBeimei,
}

func New(config *Config) *Client {
func New(config *Config) (*Client, error) {

client := &Client{Config: config, storageCfg: storage.Config{}}

Expand All @@ -72,16 +72,16 @@ func New(config *Config) *Client {
if z, ok := zonedata[strings.ToLower(config.Region)]; ok {
client.storageCfg.Zone = z
} else {
panic(fmt.Sprintf("Zone %s is invalid, only support huadong, huabei, huanan, beimei.", config.Region))
return nil, fmt.Errorf("Zone %s is invalid, only support huadong, huabei, huanan, beimei.", config.Region)
}
if len(config.Endpoint) == 0 {
panic("endpoint must be provided.")
return nil, fmt.Errorf("endpoint must be provided.")
}
client.storageCfg.UseHTTPS = config.UseHTTPS
client.storageCfg.UseCdnDomains = config.UseCdnDomains
client.bucketManager = storage.NewBucketManager(client.mac, &client.storageCfg)

return client
return client, nil
}

func (client Client) SetPutPolicy(putPolicy *storage.PutPolicy) {
Expand Down
12 changes: 10 additions & 2 deletions qiniu/qiniu_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,29 @@ func init() {
return
}

client = qiniu.New(&qiniu.Config{
var err error
client, err = qiniu.New(&qiniu.Config{
AccessID: config.Public.AccessID,
AccessKey: config.Public.AccessKey,
Region: config.Public.Region,
Bucket: config.Public.Bucket,
Endpoint: config.Public.Endpoint,
})
privateClient = qiniu.New(&qiniu.Config{
if err != nil {
panic(err)
}

privateClient, err = qiniu.New(&qiniu.Config{
AccessID: config.Private.AccessID,
AccessKey: config.Private.AccessKey,
Region: config.Private.Region,
Bucket: config.Private.Bucket,
Endpoint: config.Private.Endpoint,
PrivateURL: true,
})
if err != nil {
panic(err)
}
}

func TestAll(t *testing.T) {
Expand Down

0 comments on commit 5cbbf3b

Please sign in to comment.