@@ -2,13 +2,14 @@ package com.github.lppedd.cc.api.impl
22
33import com.github.lppedd.cc.CC
44import com.github.lppedd.cc.CCBundle
5- import com.github.lppedd.cc.CCNotification
5+ import com.github.lppedd.cc.CCNotificationService
66import com.github.lppedd.cc.api.*
77import com.github.lppedd.cc.configuration.CCTokensService
8- import com.github.lppedd.cc.configuration.SchemaValidationException
8+ import com.github.lppedd.cc.configuration.CCTokensService.TokensModel
9+ import com.github.lppedd.cc.configuration.CoAuthorsResult
10+ import com.github.lppedd.cc.configuration.TokensResult
911import com.intellij.openapi.components.service
1012import com.intellij.openapi.diagnostic.logger
11- import com.intellij.openapi.progress.ProcessCanceledException
1213import com.intellij.openapi.project.Project
1314import javax.swing.Icon
1415
@@ -25,34 +26,22 @@ internal class InternalCommitTokenProvider(private val project: Project) :
2526 private val logger = logger<InternalCommitTokenProvider >()
2627 }
2728
28- private val tokensService = project.service<CCTokensService >()
29- private val defaults
30- get() = try {
31- tokensService.getTokens()
32- } catch (e: ProcessCanceledException ) {
33- throw e
34- } catch (e: Exception ) {
35- logger.debug(" Error while reading the custom tokens file" , e)
36- notifyErrorToUser(e)
37- tokensService.getBundledTokens()
38- }
39-
4029 override fun getId (): String =
4130 ID
4231
4332 override fun getPresentation (): ProviderPresentation =
4433 DefaultProviderPresentation
4534
4635 override fun getCommitTypes (prefix : String ): Collection <CommitType > =
47- defaults .types.map { (key, value) -> DefaultCommitToken (key, value.description) }
36+ getTokens() .types.map { (key, value) -> DefaultCommitToken (key, value.description) }
4837
4938 override fun getCommitScopes (type : String ): Collection <CommitScope > {
50- val defaultType = defaults .types[type] ? : return emptyList()
39+ val defaultType = getTokens() .types[type] ? : return emptyList()
5140 return defaultType.scopes.map { DefaultCommitToken (it.name, it.description) }
5241 }
5342
5443 override fun getCommitFooterTypes (): Collection <CommitFooterType > =
55- defaults .footerTypes.map { (key, value) -> DefaultCommitToken (key, value.description) }
44+ getTokens() .footerTypes.map { (key, value) -> DefaultCommitToken (key, value.description) }
5645
5746 override fun getCommitFooterValues (
5847 footerType : String ,
@@ -61,24 +50,40 @@ internal class InternalCommitTokenProvider(private val project: Project) :
6150 subject : String? ,
6251 ): Collection <CommitFooterValue > {
6352 if (" co-authored-by" .equals(footerType, ignoreCase = true )) {
64- return tokensService.getCoAuthors()
65- .take(3 )
66- .map { DefaultCommitToken (it, " " , true ) }
53+ val tokensService = project.service<CCTokensService >()
54+
55+ when (val result = tokensService.getCoAuthors()) {
56+ is CoAuthorsResult .Success -> {
57+ return result.coAuthors.take(3 ).map { DefaultCommitToken (it, " " , true ) }
58+ }
59+ is CoAuthorsResult .Failure -> {
60+ logger.debug(" Error while getting co-authors" , result.message)
61+ }
62+ }
6763 }
6864
69- val defaultFooterType = defaults .footerTypes[footerType] ? : return emptyList()
65+ val defaultFooterType = getTokens() .footerTypes[footerType] ? : return emptyList()
7066 return defaultFooterType.values.map { DefaultCommitToken (it.name, it.description) }
7167 }
7268
73- private fun notifyErrorToUser (e : Exception ) {
74- val details = if (e is SchemaValidationException ) {
75- CCBundle [" cc.notifications.schema.validation" ]
76- } else {
77- CCBundle [" cc.notifications.schema.seeLogs" ]
78- }
69+ private fun getTokens (): TokensModel {
70+ val tokensService = project.service<CCTokensService >()
7971
80- val message = CCBundle [" cc.notifications.schema" , details]
81- CCNotification .createErrorNotification(message).notify(project)
72+ @Suppress(" LoggingSimilarMessage" )
73+ return when (val result = tokensService.getTokens()) {
74+ is TokensResult .Success -> result.tokens
75+ is TokensResult .FileError -> {
76+ logger.error(" Error while getting tokens" , result.message)
77+ tokensService.getBundledTokens()
78+ }
79+ is TokensResult .SchemaError -> {
80+ logger.debug(" Error while getting tokens" , result.failure)
81+ val details = CCBundle [" cc.notifications.schema.validation" ]
82+ val message = CCBundle [" cc.notifications.schema" , details]
83+ project.service<CCNotificationService >().notifyError(message)
84+ tokensService.getBundledTokens()
85+ }
86+ }
8287 }
8388
8489 private object DefaultProviderPresentation : ProviderPresentation {
0 commit comments