Skip to content

Commit 34adf3b

Browse files
committed
Readme - ServiceObtainError and objc
1 parent eca1732 commit 34adf3b

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

README.md

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ The default is `NSLock`, but you can choose `DispatchSemaphore` or a separate qu
432432
`Service[Params]SafeProvider` является наследником обычных провайдеров, поэтому доступен весь стандартный набор методов и можно хранить и передавать такой провайдер как обычный. Но в дополнение есть один метод - `getServiceAsResultNotSafe()`, который проигнорирует любые блокировки и выполнит обычное не безопасное получение сервиса.
433433

434434

435+
#### An example use ServiceSafeProvider:
435436
```swift
436437
struct ServiceContainer {
437438
let firstService: ServiceProvider<FirstService>
@@ -450,15 +451,64 @@ extension ServiceContainer {
450451
let service = container.firstService.getServiceAsOptional()
451452
```
452453

454+
### ServiceObtainError
455+
456+
If an error occurs as a result of getting the service, the provider returns `ServiceObtainError` with the original error and detailed information.
457+
The error will contain information about the service in whose factory the error was throwed. Since services are dependent on each other and there is nesting when getting, the error may occur when getting a dependent service, and not when getting the original one - for this purpose, the error contains information about the path to the service with the error.
458+
459+
#
460+
461+
Если в результате получения сервиса возникла ошибка, то провайдер вернет `ServiceObtainError` с исходной ошибкой и подробной информацией.
462+
В ошибке будет информацией об сервисе, в фабрике которого была получена ошибка. Т.к. сервисы зависимы между собой и при получении есть вложенность, то ошибка может возникнуть при получении зависимого сервиса, а не при запросе исходного - для этого в ошибке есть информация о пути до сервиса с ошибкой.
463+
464+
465+
#### An example get ServiceObtainError and nested services:
466+
```swift
467+
struct FirstServiceFactory: ServiceFactory {
468+
let mode: ServiceFactoryMode = .lazy
469+
func makeService() throws -> FirstService {
470+
throw SomeError()
471+
}
472+
}
473+
474+
struct SecondServiceFactory: ServiceFactory {
475+
let firstService: ServiceProvider<FirstService>
476+
477+
let mode: ServiceFactoryMode = .many
478+
func makeService() throws -> SecondService {
479+
return SecondServiceImpl(
480+
firstService: try firstService.getService()
481+
)
482+
}
483+
}
484+
485+
do {
486+
let service = try secondServiceProvider.getService()
487+
} catch {
488+
// error is ServiceObtainError
489+
// error.error is SomeError
490+
491+
// error.service = FirstService
492+
// error.pathServices = [SecondService, FirstService]
493+
// error.isNested = true
494+
}
495+
```
496+
453497

454498
### Support Objective-C
455499

456500
Creating and configuring the container is only available for swift code, but for objective-c, you can provide a special wrapper to getting the services.
457501

458502
`ServiceProviderObjC` (in Objective-C is visible as `ServiceProvider`) and `ServiceParamsProviderObjC` (in Objective-C is visible as `ServiceParamsProvider`) can be created from any `Service[Params]Provider`, passing it (swift option) to the constructor in the swift code.
459503

504+
You can get the service through selectors:
505+
- `[ServiceProvider getService]`,
506+
- `[ServiceProvider getServiceOrFatal]`,
507+
- `[ServiceProvider getServiceAndReturnError:]`,
508+
- `[ServiceParamsProvider getServiceWithParams:]`,
509+
- `[ServiceParamsProvider getServiceOrFatalWithParams:]`,
510+
- `[ServiceParamsProvider getServiceWithParams:andReturnError:]`.
460511

461-
You can get the service through selectors `[ServiceProvider getService]` and `[ServiceProvider getServiceAndReturnError:]`, also `[ServiceParamsProvider getServiceWithParams:]` and `[ServiceProvider getServiceWithParams:andReturnError:]`.
462512

463513
#### An example use ServiceProvider:
464514
```objc

0 commit comments

Comments
 (0)