Mapping Errors | raywenderlich.com

Operators such as tryMap erase any error types that are encountered during execution of the code within the operator. The mapError operator lets developers maintain knowledge of errors encountered, passing them down the Combine pipeline.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/21773708-intermediate-combine/lessons/4

hi!! there is only this part i dont understand
$0 as? NameError ?? .unknown
if $0 as? NameError fails. how come it becomes .unknown?
unknown is one case of NameError. it is not another error Type
maybe i need to review enums , and errors handling
great tutorial by the way :slight_smile:

@pedromayo This is quite a tricky one to get right because of the way in which the Combine framework behaves error wise.

You first of all use the setFailureType(to:) instance method to set the error type of the Just<String> publisher to the NameError type in case it fails since the Just<String> publisher always returns a value of some sort whatsoever and never normally fails on its very own no matter what.

You can read even more about setFailureType(to:) over here:

https://developer.apple.com/documentation/combine/fail/setfailuretype(to:)

Then you go ahead and use the tryMap(_:) instance method to throw the NameError.tooShort(String) error. This works now since you already set the Just<String> publisher error type to NameError in the previous step, so all is good so far and up to this point.

Feel free to check out even more about tryMap(_:) over here:

https://developer.apple.com/documentation/combine/fail/trymap(_:)

The only problem over here is that Combine does not actually know anything at all about and can definitely not tell anything at all regarding what type of error you throw exactly with tryMap(_:) at this point. It can be either any kind of error type out there or even no error at all after all from its point of view. On the other hand, the Just<String> publisher can only fail with NameError errors no matter what, so any other error type you throw from tryMap(_:) results in a compiler error because of that.

In order to fix this, simply just convert the error type thrown by tryMap(_:) to NameError with the mapError(_:) instance method and you are finally good to go. You either do nothing if the thrown error is already a NameError one or replace the thrown error with NameError.unknown if it has a different error type. You may use type inference for the NameError enumeration case since the Just<String> publisher definitely knows that it can only fail with NameError errors in this case.

You can check out how mapError(_:) actually works under the hood over here:

https://developer.apple.com/documentation/combine/fail/maperror(_:)

Please let me know if you have any more questions or other issues about the whole thing when you get a chance. Thank you!

For any input or output operation, a data mapping error will cause a severe error message to be issued. For blocked output, if one or more of the records in the block contains data mapping errors and the file is closed before reaching the end of the block, a severe error message is issued and a system dump is created.

When an error occurs during execution of a web application, you can have the application display a specific error screen according to the type of error. In particular, you can specify a mapping between the status code returned in an HTTP response or a Java programming language exception returned by any web component (see Handling Servlet Errors) and any type of error screen.

To set up error mappings using NetBeans IDE, do the following:

Open the project if you haven’t already.

Expand the project’s node in the Projects pane.

Expand the Web Pages node and then the WEB-INF node.

Double-click web.xml.

Click Pages at the top of the editor pane.

Expand the Error Pages node.

Click Add.

In the Add Error Page dialog:

Click Browse to locate the page that you want to act as the error page.

Enter the HTTP status code that will cause the error page to be opened in the Error Code field.

Enter the exception that will cause the error page to load in the Exception Type field.

Click OK.

Alternatively, you can click XML at the top of the editor pane and enter the error page mapping by hand using the following elements:

An exception-type element specifying either the exception or the HTTP status code that will cause the error page to be opened.

A location element that specifies the name of a web resource to be invoked when the status code or exception is returned. The name should have a leading forward slash (/).

An error-page element that encloses the previous two elements.

You can have multiple error-page elements in your deployment descriptor. Each one of the elements identifies a different error that causes an error page to open. This error page can be the same for any number of error-page elements.

This may help you,
Rachel Gomez