2013-01-07 72 views
1

我成功使用RESTKit來訪問Web服務。但我有問題將服務器錯誤(如404,403 ...)映射到RESTKit的標準RKErrorMessage類。比方說,我從我的服務提出了以下JSON響應一個403個狀態碼在一起:使用RESTKit映射服務器錯誤

{"errors":{"message":"Some error message"}} 

在我的iOS應用程序,我嘗試錯誤的幫助下圖:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class 
[errorMapping addPropertyMapping:[RKAttributeMapping 
    attributeMappingFromKeyPath:@"message" toKeyPath:@"message"]]; 

statusCodes4xx = RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError); 

errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:notecardMapping 
        pathPattern:nil keyPath:@"errors" statusCodes:statusCodes4xx]; 

但所有我get是從RESTkit發出的錯誤:

Failed mapping operation: No mappable values found for any of the attributes or relationship mappings 

我的錯在哪裏?例如,它在描述符中只是一個錯誤的路徑?

+0

我認爲你想'@「errors.message」'作爲你的密鑰 – mkral

+0

也不應該是'addAttributeMapping'? – mkral

+0

謝謝你的回答。不幸的是,「errors.message」也不起作用。我的代碼與RESTKit網站上描述的解決方案非常相似:[link](https://github.com/RestKit/RestKit)。但是他們不會爲他們的例子發佈相應的服務器響應。 – Benni

回答

6

您似乎在回覆描述符中使用了notecardMapping而不是errorMapping。試試這個:

RKObjectMapping *errorMapping = [RKObjectMapping mappingForClass:[RKErrorMessage class]]; 

[errorMapping addPropertyMapping:[RKAttributeMapping attributeMappingFromKeyPath:nil toKeyPath:@"errorMessage"]]; 

RKResponseDescriptor *errorDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:errorMapping 
                       pathPattern:nil 
                        keyPath:@"errors.message" 
                       statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassClientError)]; 

[manager addResponseDescriptorsFromArray:@[errorDescriptor]]; 
+0

的確,應該在響應描述符內進行errorMapping。此外,在描述符中作爲keyPath的「erors」實際上已經足夠了。非常感謝! – Benni